user963395
user963395

Reputation:

Javascript onchange on select inside script

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

Answers (2)

Sarfraz
Sarfraz

Reputation: 382909

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.

Upvotes: 1

yodog
yodog

Reputation: 6252

its working fine.

just change the options like this:

imagem

Upvotes: 1

Related Questions