Click here to Skip to main content
12,507,674 members (49,570 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: jQuery jquery-ui
Hi
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
k5_ce566
Updated 17-Sep-12 20:43pm
v3
Rate this: bad
 
good
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.
  Permalink  
Rate this: bad
 
good
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');
$('#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.
  Permalink  
v2

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 | Mobile
Web02 | 2.8.160927.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2016
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