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
https://forums.phpfreaks.com/topic/181860-solved-javascript-onchange-event/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.