?
Solved

Need HTML Select ID & Value?

Posted on 2014-10-28
3
Medium Priority
?
144 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 1000 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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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 how to dynamically set the form action using jQuery.
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…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

741 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