Solved

Need HTML Select ID & Value?

Posted on 2014-10-28
3
138 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

786 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