faqts : Computers : Programming : Languages : JavaScript : Event handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 32 people (56%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

Adding onkeydown event handler to <SELECT> that already has onhange handler prevents onchange event.

Mar 6th, 2003 10:14
OrbMan, Kameshwari Bhamidi,


This problem is mostly with IE.  Netscape v4.77 fires the event 
regardless.  Mozilla v1.3b will fire the onChange event, but not until 
focus leaves the control.  IE simply does not fire the event.

You can fire the onChange event manually, if necessary.  See the 
example below, which works in IE6:

<html>
<head>
<script language="javascript">

function DoNothing() {

//trigger onChange manually,
//because we return false, below
event.srcElement.onchange();

//you can also do it
//this way:
//event.srcElement.fireEvent('onchange');

//returning false is what prevents IE
//from triggering onChange
return false;
}

</script>
</head>
<body>
<form>
<select onKeyDown="return DoNothing()" onChange="alert('onChange 
fired')">
<option>one</option>
<option>two</option>
<option>three</option>
</select>
</form>
</body>
</html>