Chameleon wrote:
Chameleon wrote:
In the code below I want to alert the selected option of select.
In Mozilla the code works (If I choose "3" alerts it).
In IE alerts: nothing appear
---------------------
<script>
function hi() {
d = document.getElementById("day").value;
alert(d);
}
</script>
<select id=day onchange="hi();">
<option>0
<option>1
<option>2
<option>3
<option>4
</select>
---------------------
ok!
the answer (in this case) is equivalent with:
d = document.getElementById("day").selectedIndex;
Actually, TheBagbournes was right that you are dependent upon coincidence.
According to the W3C HTML 4 spec, if an option element doesn't have a
value attribute, then the value is its text content. But IE doesn't
follow the spec - if you don't have a value attribute, IE wants you to
explicitly get the text, so:
var sel = document.getElementById("day");
var d = sel.options[sel.selectedIndex].text;
will do the job. If you don't know whether there is a value attribute
or not, then you'll have to test for it and, if it's undefined, get the
text - something like:
var sel = document.getElementById('day');
var opt = sel.options[sel.selectedIndex];
var d = opt.value || opt.text;
Incidentally, the markup is fine for HTML - some will criticise you for
closing tags that don't need closing - cest la vie.
--
Rob