Jump to content

[SOLVED] Javascript onChange event?


Perad

Recommended Posts

I want the following to work.

 

document.getElementById('country').onChange = function() {
alert(1);
}

 

Basically, when the country select box is changed I want something to happen. I do not want to use the onchange attribute within the html. I would rather have this functionality separate. Once I find out how to do this I can do the rest.

 

Help is very much appreciated.

Link to comment
Share on other sites

Read the comments for explanation

<script type="text/javascript">

// wait for the complete page to be loaded (less efficient then DOM ready but you'll need a function for that)
window.onload = function(){
    // get the select element
    var el = document.getElementById('country');

    // add an onchange event note that javascript is case sensitive and the "c" should be lowercase
    el.onchange = function() {

        // I prefer Firebug's console.log over alert less anoying change it to alert if you like
        console.log("value changed to: "+el.value);
    }
}
</script>
<select id="country">
    <option value="1">Aruba</option>
    <option value="2">Bonaire</option>
    <option value="3">Curacao</option>
</select>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.