473,320 Members | 1,900 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,320 software developers and data experts.

How do you retrieve a <select> selected option in javascript ?

Hi,

I need to check a form (consisting mostly of select elements) and ensure that none of the values are empty/null.

Unfortunately I don't know how many select elements there will be so I need to be able to loop through as many items as there could possibly be.

Form example;-
Expand|Select|Wrap|Line Numbers
  1. <form id='FormID' action='action.php'>
  2.   <select name='select1'>
  3.     <option>Option 1</option>
  4.     <option>Option 2</option>
  5.     <option>Option 3</option>
  6.   </select>
  7.  
  8.   <select name=select2'>
  9.     <option>Option 1</option>
  10.     <option>Option 2</option>
  11.     <option>Option 3</option>
  12.   </select>
  13.  
  14.   <select name='select3'>
  15.     <option>Option 1</option>
  16.     <option>Option 2</option>
  17.     <option>Option 3</option>
  18.   </select>
  19. </form>
  20.  
I had written a piece of code;-
Expand|Select|Wrap|Line Numbers
  1.    var elem = document.getElementById('FormID').elements;
  2.       for(var i = 0; i < elem.length; i++)
  3.         {
  4.           if (elem[i].value == "")
  5.             {
  6.               alert("One of the fields was blank");
  7.               return;
  8.             }
  9.         }
  10.  
This works fine in firefox, but the select values are always blank in IE8 (the browser that will be using this site mostly).

Can anyone please help me adjust the code above to properly check the fields in IE ?

Many thanks
Phil
Sep 29 '11 #1
3 1897
Dormilich
8,658 Expert Mod 8TB
IE requires the value attribute in the option tag to work like that. i.e.
Expand|Select|Wrap|Line Numbers
  1.   <select name='select1'>
  2.     <option value="Option 1">Option 1</option>
  3.     <option value="Option 2">Option 2</option>
  4.     <option value="Option 3">Option 3</option>
  5.   </select>
Sep 29 '11 #2
Thanks Dormlich for your prompt, concise and helpful response !
Worked a treat :-)
Sep 29 '11 #3
Dormilich
8,658 Expert Mod 8TB
it’s a known problem, you just have to know the right google keywords to look it up again.
Sep 29 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ang Talunin | last post by:
Hey, I wondering if it's possible to retrieve all the <option>-fields from a <select> when posting a <form> to a php file Example: I've got a form like this: <form action = phpfile.php...
2
by: Joachim Bauer | last post by:
I'm using the code below to display a menu that opens when the mouse goes over the main menu item (try it in your browser to understand the behaviour). It uses "position:absolute" and a switch...
2
by: Aaron C | last post by:
This is kindof a newbie question, so please bear with me... I need to pass the selected option of a <select> tag into a query string, so that the page is reloaded with the selected option already...
6
by: Omar | last post by:
Hi, In a JSP I have the next: .... codigo = "<select name='" + nombre + "'>\n<option selected value='default'>Escoge</option><option value='todos'>Todos</option>"; if (miRS != null) while...
6
by: Bonge Boo! | last post by:
This has got to be obvious, but I can't make it work. I have a form called with 3 pull down menus. They are linked to a database which generates the values for the <SELECT? Pull-downs. Lets...
14
by: xxbmichae1 | last post by:
I have a <select> object that i've set up an onchange event that fires in IE fine when I use the cursor up and down in the list, but If I use the cursor up and down in Firefox the event doesn't...
6
by: joseph.lindley | last post by:
Forgive me for I am a bit of a web-dev novice - but I'm not doing too bad. I'm currently working with a bit of javascript to dynamically add <option>s into a select box. My code currently works...
1
by: frey | last post by:
i tried to create a dropdown menu and use the option as a control to change content inside another text area the code is like this: <select name="xxxx"><option onclick="changeunitprice(29.87)"...
1
by: ghadley_00 | last post by:
Hi, I have a php form that encodes the responses to various <Select> field drop down menus as numbers (e.g. 0 to 50), and then posts the data to a MySQL table. I am trying to implement a form...
5
by: = poster = | last post by:
Hi all , I have a script which let the user choose between four days : Day: <select name=\"day\" value=\"$day\"> <option value=\"01\">1</option> <option value=\"02\">2</option> <option...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.