Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > get the selected OPTION from SELECT

Reply
Thread Tools

get the selected OPTION from SELECT

 
 
Chameleon
Guest
Posts: n/a
 
      03-11-2006
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>
---------------------
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      03-11-2006
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.
 
Reply With Quote
 
 
 
 
Chameleon
Guest
Posts: n/a
 
      03-11-2006
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;
 
Reply With Quote
 
TheBagbournes
Guest
Posts: n/a
 
      03-11-2006
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*!
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      03-12-2006
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/
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-12-2006
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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-13-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
selected=selected in the center of option list John HTML 5 03-01-2010 08:30 PM
Reset Selected Item in Select Option List Dennis Marks HTML 1 09-08-2006 08:21 PM
How to determine whether the user has already selected an option from a multi select list Patrick Olurotimi Ige ASP .Net 6 03-11-2005 10:39 AM
Dynamically update SELECTED option in option list Mark Kolber Javascript 4 01-01-2005 02:07 PM
Selection from One SELECT changes selected option of another SELECT? J. Hall HTML 2 04-21-2004 05:36 PM



Advertisments