Solved

Need HTML Select ID & Value?

Posted on 2014-10-28
3
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 29

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

696 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