Reputation:
I'm learning Javascript, and here's what I'm trying to do...
<body>
<select name="selTitle" id="titles">
<option value="Mr.">Mr.</option>
<option value="Ms.">Ms.</option>
</select>
</body>
and for the script,
<script type='text/javascript'>
var title = document.getElementById("titles");
title.onchange = function() {
alert("Hi");
}
</script>
But its not working, am I doing something wrong? Here's the demo.. http://jsfiddle.net/jq9UA/10/
Upvotes: 0
Views: 3188
Reputation: 382909
Wrap your code in onload
event:
window.onload = function(){
var titles = document.getElementById("titles");
titles.onchange = function() {
alert("Hi");
}
};
This makes sure that select box is actually available to apply events to which you can do using onload
event or puting your script at bottom of your page just before </body>
tag.
Upvotes: 1