473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 64612
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Serge Myrand | last post by:
Hi, I cannot get the selected option to be POSTed (it does not appear in the QueryString when using GET neither) I construct the drop down list from an array, I select an element in the list,...
1
by: ouioui | last post by:
Hi, I try to have a communicatition between 2 windows In the first one i have a HTML styled select like that : <form name='addform' id='addform' action='add_dossier.php' method='post' >...
4
by: Mark Kolber | last post by:
I did a little searching on this but couldn't find an answer... On my website, I have a section of stories (www.midlifeflight.com/stories) There are different stores on different pages that are...
8
by: McKirahan | last post by:
Firefox does not reflect selected option via innerHTML How do I get Firefox to reflect selected option values? <html> <head> <title>FFinner.htm</title> <script type="text/javascript">...
2
by: Monty | last post by:
I use a SELECT dropdown as the nav interface for jumping to a chosen page number. When I setup up the SELECT element on the page, I want to show the user the current page number they are on, so, I...
3
by: Xerxes | last post by:
Hi, I have 3 images of the same product in 3 different colors. I want to display the correct color product based on what color is selected using a dropdown. Can you please tell me how I can do...
1
by: vraamu | last post by:
here in this option transfer code.after transfering data from available _services multiple box to assigned_services multiple box i am unable to get the value of the assigned_services data for ex in...
10
by: puT3 | last post by:
I want to get the value from Product_Price field which it will display it record on a text box based on a product that is selected from a list box. Product Table Product_ID Product_Name...
2
by: rkyakkala | last post by:
Hi, Please see the below html code.i am able to get whatever selected in the drop down list box. But i need to see the title name of the window as it is selected could you please suggest in this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.