Perad Posted November 17, 2009 Share Posted November 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted November 17, 2009 Share Posted November 17, 2009 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> Quote Link to comment Share on other sites More sharing options...
Perad Posted November 18, 2009 Author Share Posted November 18, 2009 Thanks for your help. That worked perfectly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.