How do I pull the selection from a dropdownlist using jQuery?

1

I do not have much knowledge of jQuery, I would like to know how to get the selected attribute of the dropdownlist using jQuery?

    
asked by anonymous 11.05.2016 / 17:28

1 answer

1

With native JavaScript:

With native JS you have to find out the option that is selected. You can get this information through select.selectedIndex , which indicates the index of the options of a selected dice that is selected.

To find and de-select you can do this:

var select = document.querySelector('select');
options = select.querySelectorAll('option');
options[select.selectedIndex].removeAttribute("selected");

jsFiddle: link

With jQuery:

jQuery saves a few lines and lets you do this:

$('option:selected').removeAttr('selected');

jsFiddle: link

Making $('select').val(''); visually changes the state but the attribute is still there.

    
11.05.2016 / 17:43