B. Clay Shannon B. Clay Shannon - 5 months ago 7x
Javascript Question

Why does this work in javascript but not when jquerified?

I have some html selects:

<select class="form-control, dropdown" id="delperffrom" name="delperffrom">
. . .
<select class="form-control, dropdown" id="delperfto" name="delperfto">
. . .

I want to initialize them to "no value" and so have this javascript, which works:

document.getElementById("delperffrom").selectedIndex = -1;

..and this jQuery, which doesn't:

$("#delperfto").selectedIndex = -1;

Why does the jQuery not work?


The .selectedindex is property of the DOM element, not jQuery.So you need change it on DOM element, or using jQuery .prop() method:

$("#delperffrom")[0].selectedIndex = -1; // change on dom element pulled from jQuery
$("#delperffrom").prop('selectedIndex',-1); // change with .prop() jquery method