How to take the selection out of a dropdownlist using jQuery?

Asked

Viewed 799 times

1

I’m not so familiar with jQuery, I’d like to know how to get the attribute selected dropdownlist using jQuery?

1 answer

1


With native Javascript:

With native JS you must discover the option which is selected. You can obtain this information through select.selectedIndex, which indicates which index of the options of a given select is selected.

To find and de-select you can do so:

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

jsFiddle: https://jsfiddle.net/L7b0hgLx/

With jQuery:

jQuery saves some lines and lets you do so:

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

jsFiddle: https://jsfiddle.net/L7b0hgLx/1


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.