Solved

Need HTML Select ID & Value?

Posted on 2014-10-28
3
132 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now