javascript - reset a select element - Stack Overflow

how to make the first option selected? I tried this but doesnt work: selectElement.options[0].selected=

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
Add a ment  | 

2 Answers 2

Reset to default 6

Try 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

相关推荐

  • javascript - reset a select element - Stack Overflow

    how to make the first option selected? I tried this but doesnt work: selectElement.options[0].selected=

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信