gene b.
gene b.

Reputation: 12026

jQuery Preselection of Dropdown Doesn't Enter Change() Function

Suppose I have

$('#myDropdown').change (function() {
   // perform some action based on some logic for the selection
});

// Preselect a certain value on startup
$('#myDropdown').val('Item 3');

When I preselect like that, the Change event never gets called, so I don't get my results.

I can certainly make the manual changes required to accompany the selection of 'Item 3' on startup but I wish everything could be as generic as possible.

Upvotes: 0

Views: 58

Answers (3)

Pengcheng
Pengcheng

Reputation: 323

Are you using a jQuery dropdown plugin, or you use the html , if you could put more codes, will be helpful.

Upvotes: 0

Sarantis Tofas
Sarantis Tofas

Reputation: 5167

the change event will fire only if a change to the element's value is committed by the user.

So you might need to trigger or call change() manually

Upvotes: 2

dontanios
dontanios

Reputation: 333

Make sure to preselect the value after it becomes available.

$( document ).ready(function() {
    // Preselect a certain value on startup
    $('#myDropdown').val('Item 3');
});

Upvotes: 0

Related Questions