469,270 Members | 1,113 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

get the selected OPTION from SELECT

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>
---------------------
Mar 11 '06 #1
6 64383
On 11/03/2006 19:31, Chameleon wrote:

[Getting selected option element]

Read the group FAQ: <http://www.jibbering.com/faq/>

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Mar 11 '06 #2
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;
Mar 11 '06 #3
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;


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*!
Mar 11 '06 #4
TheBagbournes said the following on 3/11/2006 4:37 PM:
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;


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.

<URL: http://jibbering.com/faq/#FAQ4_13 >
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 12 '06 #5
TheBagbournes wrote:
Chameleon wrote:
Chameleon wrote:
<script>
The required `type' attribute is missing:

<script type="text/javascript">

<URL:http://validator.w3.org/>
function hi() {
d = document.getElementById("day").value;
alert(d);
}
</script>
<select id=day onchange="hi();">
Attribute values should be quoted always.
<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;


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
Mar 12 '06 #6
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
Mar 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mark Kolber | last post: by
3 posts views Thread by Xerxes | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.