Jess
Jess

Reputation: 39

Get element of select value

I have a HTML select drop down that is populated from a JQuery get request. You can view that here https://codepen.io/anon/pen/xjVjra

I am trying to get the following example element of the selected value on each change.

<small class="text-muted">ETH</small>

I have tried the following but that would just bring back the name of the selected option, which is not what I am after.

$(document).ready(function () {
    $('#dropdown').on('change', function() {
        alert( $(this).val()
    });
});

Is it possible to drill to the inner code of the selected option and retrieve that data.

Thanks

Upvotes: 0

Views: 149

Answers (2)

user2638618
user2638618

Reputation:

I don't see how the question fits with the CodePen, but you could try something like this:

$("#cryptos :selected").attr("data-subtext");

Upvotes: 1

samanime
samanime

Reputation: 26615

I don't see any <small> tags in the HTML, as their shouldn't be because only <option> and <optgroup> are valid elements in a <select>.

If you meant <option>, you can use the :selected pseudo-class to get the actual <option> element instead of just its value:

// in select onchange where this == select element
$(this).find(':selected'); // the option element

Also, note that your CodePen example doesn't actually have the dropdown named #dropdown, so just make sure you use the appropriate selector.

Upvotes: 1

Related Questions