CDaly CDaly - 1 year ago 56
HTML Question

Javascript select on change doesn't fire first try

I have the HTML code:

<select id = "dropdownList" onchange = "dropdownChange()">
<option> Choose a vendor </option>

With the function:

function dropdownChange(){



The dropdown list is already populated from an array in another function.When first selecting an item from the dropdown, it doesnt fire. However, any changes following the first do fire.

How can I edit my code so that when I first select an item from the dropdown it will fire right away?

rid rid
Answer Source

This is how your current code works:

  1. The first time the change event happens, dropdownChange() runs, because it's called in the element's onchange attribute.

  2. dropdownChange() now registers an event listener and returns, doing nothing else.

  3. On the next change, the event listener registered by dropdownChange() runs and calls changeVendor(this.value).

You probably want:

function dropdownChange() {

You can also call changeVendor(this.value) directly in onchange, if you'd like to avoid creating the dropdownChange() function just to call changeVendor() in it:

<select onchange="changeVendor(this.value)">
    <option>Choose a vendor</option>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download