Connecting Tech Pros Worldwide Forums | Help | Site Map

how to get selected item or value of html-combo box through javascript?

nirmalsingh's Avatar
Familiar Sight
 
Join Date: Sep 2006
Location: Madurai - India
Posts: 217
#1: Dec 30 '06
i have a combo box with id="combo1", i want to get the selected item or value through javascript. help me plz...

Member
 
Join Date: Dec 2006
Location: United States
Posts: 43
#2: Jan 1 '07

re: how to get selected item or value of html-combo box through javascript?


I don't know what you want to get the value with but here is an example with a button:


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. function combofunction(the_value){
  5.  
  6. // actions that you want to perform
  7.  
  8. }
  9. </script>
  10. </head>
  11. <body>
  12. <select name="htmlcombo">
  13.     <option name="box1" value="Value1">Display Text1</option>
  14.     <option name="box2" value="Value2">Display Text2</option>
  15.     <option name="box3" value="Value3">Display Text3</option>
  16. </select>
  17. <input type="button" name="combovalue" value="Get Combo Value" onclick="combofunction(htmlcombo.value)">
  18. </body>
  19. </html>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Jan 1 '07

re: how to get selected item or value of html-combo box through javascript?


Quote:

Originally Posted by nirmalsingh

i have a combo box with id="combo1", i want to get the selected item or value through javascript. help me plz...

Use the following code:
Expand|Select|Wrap|Line Numbers
  1. var combo1 = document.getElementById("combo1");
  2. var val = combo1.options[combo1.selectedIndex].text
If you have the form name, use that instead of getElementById.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Jan 1 '07

re: how to get selected item or value of html-combo box through javascript?


Quote:

Originally Posted by nirmalsingh

i have a combo box with id="combo1", i want to get the selected item or value through javascript. help me plz...

Easiest way is the following where your JS routine is triggered when you select an item from the drop down box. No need for a submit button.
Expand|Select|Wrap|Line Numbers
  1. function getCombo1(sel) {
  2.   var value = sel.options[sel.selectedIndex].value;  
  3. }
  4. <select id="combo1" onchange="getCombo1(this)">
  5. <option value="">Select combo</option>
  6. <option value="Value1">Text1</option>
  7. <option value="Value2">Text2</option>
  8. <option value="Value3">Text3</option>
  9. </select>
Ronald :cool:
Familiar Sight
 
Join Date: Nov 2007
Posts: 243
#5: Jun 30 '09

re: how to get selected item or value of html-combo box through javascript?


Quote:

Originally Posted by acoder View Post

Use the following code:

Expand|Select|Wrap|Line Numbers
  1. var combo1 = document.getElementById("combo1");
  2. var val = combo1.options[combo1.selectedIndex].text
If you have the form name, use that instead of getElementById.

I have modified this code like this
Expand|Select|Wrap|Line Numbers
  1. <input type="button" onclick="change(selectname.options[selectname.selectedIndex].value)"
  2.  
and passing to the click event of button. code works fine in IE but this does not work in Firefox Mozilla.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Jun 30 '09

re: how to get selected item or value of html-combo box through javascript?


That's because selectname will be undefined. IE is wrong here. You need to either define selectname, or write it out in full twice.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Jun 30 '09

re: how to get selected item or value of html-combo box through javascript?


Quote:

Originally Posted by acoder View Post

If you have the form name, use that instead of getElementById.

I did type this on January the 1st, 2007, but now I'd say that you should use document.getElementById(), though using the form name via document.forms[] is also valid. I'd also add that you can use the following code (as long as you have set the values of each of the options):
Expand|Select|Wrap|Line Numbers
  1. var combo1 = document.getElementById("combo1").value;
Familiar Sight
 
Join Date: Nov 2007
Posts: 243
#8: Jun 30 '09

re: how to get selected item or value of html-combo box through javascript?


Quote:

Originally Posted by acoder View Post

I did type this on January the 1st, 2007, but now I'd say that you should use document.getElementById(), though using the form name via document.forms[] is also valid. I'd also add that you can use the following code (as long as you have set the values of each of the options):

Expand|Select|Wrap|Line Numbers
  1. var combo1 = document.getElementById("combo1").value;

thanks for kind effort to solve the problem!
Reply


Similar JavaScript / Ajax / DHTML bytes