get the selected OPTION from SELECT

C

Chameleon

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>
---------------------
 
C

Chameleon

Chameleon said:
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;
 
T

TheBagbournes

Chameleon said:
ok!
the answer (in this case) is equivalent with:
d = document.getElementById("day").selectedIndex;

Well, it just happens to be the case. What if the *values* of the
options are not 0, 1, 2, 3 etc?

And CLOSE YOUR TAGS! That markup is *hideous*!
 
R

Randy Webb

TheBagbournes said the following on 3/11/2006 4:37 PM:
Well, it just happens to be the case. What if the *values* of the
options are not 0, 1, 2, 3 etc?

gEBI isn't the best way to access forms anyway.

And CLOSE YOUR TAGS! That markup is *hideous*!

The only thing hideous about that markup, with regards to the
select/options, is your perception of what is hideous.
 
T

Thomas 'PointedEars' Lahn

The required `type' attribute is missing:

<script type="text/javascript">


Attribute values should be quoted always.
Well, it just happens to be the case.

No, it is not.
What if the *values* of the options are not 0, 1, 2, 3 etc?

What if document.getElementById() is not supported? Rare nowadays, but
still possible.
And CLOSE YOUR TAGS! That markup is *hideous*!

(Please do not SHOUT. There are *other* ways to /emphasize/ text.)

A matter of taste regarding the HTML `option' element, since its end tag
is optional even in HTML 4.01 Strict. I agree here, especially for XHTML
(because not well-formed is, in a sense, hideous), and for the rest,
though.


PointedEars
 
R

RobG

Chameleon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top