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

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

nirmalsingh
218 100+
i have a combo box with id="combo1", i want to get the selected item or value through javascript. help me plz...
Dec 30 '06 #1
9 158215
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>
Jan 1 '07 #2
acoder
16,027 Expert Mod 8TB
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.
Jan 1 '07 #3
ronverdonk
4,258 Expert 4TB
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:
Jan 1 '07 #4
mukeshrasm
254 100+
@acoder
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.
Jun 30 '09 #5
acoder
16,027 Expert Mod 8TB
That's because selectname will be undefined. IE is wrong here. You need to either define selectname, or write it out in full twice.
Jun 30 '09 #6
acoder
16,027 Expert Mod 8TB
@acoder
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;
Jun 30 '09 #7
mukeshrasm
254 100+
@acoder
thanks for kind effort to solve the problem!
Jun 30 '09 #8
button:
Expand|Select|Wrap|Line Numbers
  1. <input name="btnGo" value=" Search " class="seinput" type="submit" onclick="javascript:submitSearch();">
select option:
Expand|Select|Wrap|Line Numbers
  1. <select name="category" class="catpinput">
  2.                     <option value="-1" selected="selected">All Category</option>
JS Function:
Expand|Select|Wrap|Line Numbers
  1. function submitSearch()
  2. {
  3. var catkey = $('.catpinput').val();
  4. window.alert(catkey);
  5. }
Oct 14 '10 #9
acoder
16,027 Expert Mod 8TB
That's not JavaScript (OK, technically, it is, but it requires jQuery which you should mention).

There's also no need to use the "javascript:" protocol in an onclick.
Oct 14 '10 #10

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

Similar topics

4
by: Peter Moscatt | last post by:
I am having trouble understanding the methods for the Listbox from Tk. If I was to select at item in the list using a mouse click (have already created the bind event) - what method returns the...
3
by: Stanley J Mroczek | last post by:
I have tried this with no luck. What i want is to show the selected item on the first post. the selected item is NY but the first in the list shows as selected. If Page.IsPostBack = False Then...
1
by: Marc Jennings | last post by:
Hi There is probably a very simple answer to this, but I am not too familiar with the treeView control... I have a treeview control, populated from a database. I want to be able to get the...
5
by: Kris Rockwell | last post by:
Hello (again), I have gotten the dropdown list functionality to work through a few tricks (probably not the most efficient, but it works) but I am not sure how to set the default selected value....
3
by: Randy Rubin | last post by:
Does anyone know how to Make an Item selected in a DropDownListBox when Binding it to data. When it changes it populates another TextBox but the DDLB does not retain it's selected value. Thanks...
4
by: juststarter | last post by:
Hello, I have an aspx file where i've put a placeholder element. On load (page_load) i create dynamically an html table which contains a checkbox and a radiobuttonlist in each tablerow . The...
5
by: David | last post by:
I want to get the Sel_142's multiple item's value, and put them in an array. if ( !Sel_142.Multiple ) vo.CA142 = Sel_142.Items.Value; else { Sel_142.Items..... }
6
by: George | last post by:
Hi all, How can I get the value stored from the selected item and subitems of a listview? Thanks in advance, George
0
by: Eirik Eldorsen | last post by:
I can't figure out how to set that an level1 and level2 item is selected at the same time. Is this not possible? Here is my code that set selected item: private void UpdateMenu() { foreach...
4
by: Ronny Mandal | last post by:
Hi! I have an .aspx with some controls that are created dynamically. The items are populated into the box by setting the DataSource-property to a list. In addition I specify the text and value...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.