Click here to Skip to main content
Sign Up to vote bad
good
See more: jQueryJQueryUI
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:37
k5_ce504
Edited 17 Sep '12 - 20:43


2 solutions

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  
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 425
1 OriginalGriff 315
2 Slacker007 240
3 Maciej Los 240
4 Aarti Meswania 210
0 Sergey Alexandrovich Kryukov 8,953
1 OriginalGriff 7,134
2 CPallini 3,758
3 Rohan Leuva 3,036
4 Maciej Los 2,488


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid