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".

Recommended Answers

All 2 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.