...

View Full Version : Resolved getElementById('ID).value not working in ie8



javanew
09-30-2011, 06:46 PM
I have some simple javascript code that works fine in chrome and safari, but not Ie8



<head><script type="text/javascript">
function val2() {
//var num = Number(document.getElementById('PdRows').value)
alert (document.getElementById('PdRows').value)
}
</script></head>

<form action="" method="post">
<select name="PD" id="PdRows" onchange="val2()">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</form>


Why? And how do I correct?

xelawho
09-30-2011, 06:58 PM
actually, I'm going to side with IE8 on that one :eek:

you're not assigning any value to your options, and then you're asking for that value to be displayed

obviously those other browsers go looking for the value, don't find it and content themselves with the option *text* instead. IE just gives up.

try this instead:



<form action="" method="post">
<select name="PD" id="PdRows" onchange="val2()">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
</select>
</form>

although if what you want is actually the option text, that's a different story

Philip M
09-30-2011, 07:04 PM
To capture the text of a select option:-


var si = document.getElementById("PdRows").selectedIndex;
var txt1 = document.getElementById("PdRows").options[si].text;
alert (txt1);

javanew
09-30-2011, 09:36 PM
That was the issue.. thanks!!!


actually, I'm going to side with IE8 on that one :eek:

you're not assigning any value to your options, and then you're asking for that value to be displayed

obviously those other browsers go looking for the value, don't find it and content themselves with the option *text* instead. IE just gives up.

try this instead:



<form action="" method="post">
<select name="PD" id="PdRows" onchange="val2()">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
</select>
</form>

although if what you want is actually the option text, that's a different story



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum