0

How do I get an item in my drop down menu? I know with .value I can get the value of the item selected, but what I need is the name of the selected item. For example, I have this drop down menu with contains two items: Class 1 and Class 2. The value of Class 1 is 50 and the value of Class 2 is 60. If I use DMT5.value (Class 1's value) I get 50, but what I need is the name itself which is "Class 1".

2
Contributors
2
Replies
38
Views
1 Month
Discussion Span
Last Post by essential
0

Ive used this code:

var classRate = document.getElementById("DMT5")
var selectedClassRate = classRate.options[classRate.selectedIndex].text

However, I get error: Unabled to get property 'text' of undefined or null reference

0

Hi,
I'm not sure if this is what you seek for. But here's a way to do it somehow:
assuming we have the following SELECT element with a class of "parent".

jQuery(document).ready(function($){
$('.parent').children('option').each(function(index,item){
    $(this).on('click',function(e){
        e.preventDefault();
        // This will return the text instead of its value
        console.log($(this).text());
    });
});
});

my bad i didnt read the entire post.

Edited by essential: typographical error

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.