admin管理员组文章数量:1336311
I have a DropDown list named as batches.
If I selected 2nd option,dropdown.selectedIndex inside the OnChange function always shows the selected index.
But document.getElementById("batches").selectedIndex always shows the 1st index.
Why is this?
Actually I want read the correct selectedIndex of batches in another function that's why I need a way to get the correct selected index in both ways.
function OnChange(dropdown){
var myindex = dropdown.selectedIndex;// This prints correctly
alert("Index : "+document.getElementById("batches").selectedIndex);// This is always 0 no metter what selects
}
<select name='batches' id='batches' onchange='OnChange(this);'>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
</select>
I have a DropDown list named as batches.
If I selected 2nd option,dropdown.selectedIndex inside the OnChange function always shows the selected index.
But document.getElementById("batches").selectedIndex always shows the 1st index.
Why is this?
Actually I want read the correct selectedIndex of batches in another function that's why I need a way to get the correct selected index in both ways.
function OnChange(dropdown){
var myindex = dropdown.selectedIndex;// This prints correctly
alert("Index : "+document.getElementById("batches").selectedIndex);// This is always 0 no metter what selects
}
<select name='batches' id='batches' onchange='OnChange(this);'>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
</select>
Share
Improve this question
edited Dec 16, 2013 at 10:19
BenMorel
36.7k51 gold badges205 silver badges336 bronze badges
asked Aug 8, 2012 at 0:24
namalfernandolknamalfernandolk
9,13415 gold badges69 silver badges119 bronze badges
2
- In which browser? Firefox and IE give the correct index every time. – RobG Commented Aug 8, 2012 at 2:58
- Hi All! I'm genenerating this dropDown box dynamically using php. In that case, another dropdown box with the same name and ID has been added in another area.That's why it getting the value of it. Thanx for your effort and advices. Shame on me for not seeing that mistake! – namalfernandolk Commented Aug 8, 2012 at 4:04
2 Answers
Reset to default 1I don't know what browser you are testing in, but the following always shows true in all browsers I tested in:
<select id="batches" onchange="
alert(this.selectedIndex == document.getElementById('batches').selectedIndex);
">
<option value = "1">1
<option value = "2">2
<option value = "3">3
</select>
<!-- and to confirm... -->
<button onclick="
alert(document.getElementById('batches').selectedIndex);
">Show selected index</button>
I hope you aren't being confused by having options values 1, 2 and 3 correlate to selectedIndexes 0, 1 and 2.
Because you call the function onChange event i.e not in the past. Try to trigger the function without onchange event and the property is selected in the past
<select name='batches' id='batches' onchange='someFunc();'>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
</select>
<a href="javascript:someFunc()">Test</a>
<script>
function someFunc(){
//var myindex = dropdown.selectedIndex;// This prints correctly
alert("Index : "+document.getElementById("batches").selectedIndex);// This is always 0 no metter what selects
}
</script>
it will work. Just copy and paste this code into your text editor and test it
版权声明:本文标题:html select - DropDown List SelectedIndex is not working with getElementById in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742337047a2455833.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论