Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: jQuery JQueryUI
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_ce504
Edited 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
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Sergey Alexandrovich Kryukov 196
3 Mika Wendelius 185
4 CPallini 170
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Sergey Alexandrovich Kryukov 196
3 Mika Wendelius 185
4 CPallini 170


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2014
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