Solved

Need HTML Select ID & Value?

Posted on 2014-10-28
3
139 Views
Last Modified: 2014-10-29
I'm using this jquery to find the selected option of ten select controls. It cannot return both ID & Value.

$("#form1 select option:selected").each(function ()
{                                    
       var value = $(this).val();
       var id = $(this).attr("id");
       
       alert(id);
       alert(val); //works
});


[example]
    <select class="form-control" id="number" name="number">
    <option>Any</option>
    <option>Plural</option>
    <option>Singular</option>
    </select>
0
Comment
Question by:WorknHardr
3 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 40409888
work fine for me on chrome, safari and firefox : http://jsfiddle.net/2tpqrL26/1/

$(document).ready(function() {
    $("#form1 select option:selected").each(function () {                                    
           var value = $(this).val();
           var id = $(this).attr("id");
           alert(id);
           alert(value); //works
    });
});

Open in new window

0
 
LVL 28

Accepted Solution

by:
Pravin Asar earned 250 total points
ID: 40409898
I am not positive, about the complete applications, but if your looking to get value and text for each option of select box.

Following simple code should work. This has no external dependencies. Pure JavaScript

<select id="MySelect">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>

var e = document.getElementById("MySelect");

for (var ix=0; ix < e.options.length; ix++) {
    var optText = e.options[ix].text;
    var optValue = e.options[ix].value;
    if (ix == e.selectedIndex) {
        alert ("\nSelected Option Value:" + optValue + "\nText: " + optText);  
    }
    else {
        alert ("\nOption  Value:" + optValue + "\nText: " + optText);  
    }
}
0
 

Author Closing Comment

by:WorknHardr
ID: 40411288
I rebooted the pc this morning and now it works. I don't get it. Also tried the new code and I like it too. thx...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question