Click here to Skip to main content
13,095,445 members (62,428 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a select element in my .aspx page:
<select id="t"></select>

I want to create option elements dynamically using Jquery:
$("<option>bb</option>").attr("value", "1").attr("selected", "selected").appendTo('#t');

I want to have one item as selected item when my select element load,but when i use code above I can`t see this result,in IE no item will show as selected element and in Fire Fox I can always see the first element.
What is the problem?What can I do?
Please help me.
Thanks in advance
Posted 17-Sep-12 20:37pm
Updated 17-Sep-12 20:43pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Pass 'true' in place of value,

.attr("selected", true)

Also, I suggest using the latest .prop() instead of .attr()

.prop("selected", true)

Hope it helps.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Thanks for your answer.
I use these methods:
$("<option> bb</option>").attr("value", "1").attr("selected", true).appendTo('#t');

$("<option> bb</option>").attr("value", "1").prop("selected", true).appendTo('#t');


All of them work in Fire Fox,but my problem is in IE.I can`t see the result in IE.
I don`t know what to do?
Please help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100