how to make the first option selected? I tried this but doesnt work:
selectElement.options[0].selected=true;
it gives me this error:
Uncaught TypeError: Cannot read property '0' of undefined
update
solved! by defining selectElement correctly.
how to make the first option selected? I tried this but doesnt work:
selectElement.options[0].selected=true;
it gives me this error:
Uncaught TypeError: Cannot read property '0' of undefined
update
solved! by defining selectElement correctly.
Share Improve this question edited Feb 4, 2012 at 15:36 Sami Al-Subhi asked Feb 4, 2012 at 15:27 Sami Al-SubhiSami Al-Subhi 4,69010 gold badges47 silver badges75 bronze badges 2-
3
How have you defined
selectElement
? – JJJ Commented Feb 4, 2012 at 15:28 - it's my bad. I feel so stupid. selectElement refers to the element's value not the element itself. thanks for the hint. – Sami Al-Subhi Commented Feb 4, 2012 at 15:35
2 Answers
Reset to default 6Try this
document.getElementById('mySelect').selectedIndex = 0;
Try this (See the demo)
HTML
<select id="selectElement">
<option>First</option>
<option selected>Second</option>
</select>
JavaScript
selectElement.options[0].selected="selected";
Or you can do selectElement.options[0].selected=true;
Hope this works for you.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399926a4572335.html
评论列表(0条)