I’m trying to create an onchange event using javascript on a select element. But its not working, any hint what I’m doing wrong?
Script..
<script type="text/javascript">
function check() {
alert("Hi");
}
</script>
HTML..
<body>
<select name="selTitle" id="titles" onchange="check();">
<option value="Mr.">Mr.</option>
<option value="Ms.">Ms.</option>
</select>
</body>
Demo – http://jsfiddle.net/jq9UA/1/
The problem in the Fiddle is that the JavaScript code is running in response to the window load event (default on fiddle). Any code you run is defined in the context of the load call back and not at the global scope. If you look through the actual generated HTML for the frame you will find it looks like this
This means your code defines a
checkfunction which is local to the load callback but the event handler wants to invoke a globally definedcheckfunction.To fix this you need to use one of the fiddle settings that defines the JavaScript in a global context. Try “no wrap (head)”.
Fiddle: http://jsfiddle.net/jq9UA/3/