Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a select element in my .aspx page:
HTML
<select id="t"></select>

I want to create option elements dynamically using Jquery:
JavaScript
$("<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
Updated 17-Sep-12 20:43pm
v3

Pass 'true' in place of value,

JavaScript
.attr("selected", true)


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

JavaScript
.prop("selected", true)


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

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

JavaScript
$('#t').val('1');

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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900