0

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/

2 Answers 2

1

Wrap your code in onload event:

window.onload = function(){
     var titles = document.getElementById("titles");
       titles.onchange = function() {
       alert("Hi");
     }
};

Working Demo

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.

Sign up to request clarification or add additional context in comments.

Comments

1

its working fine.

just change the options like this:

imagem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.