最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - reset a select element - Stack Overflow

programmeradmin4浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论