b_*****@yahoo.ca said on 23/03/2006 1:58 AM AEST:
I realize that the onChange event for a drop down can be trapped as
follows:
<select name="myDropDown" onChange="somefunc">
Maybe you mean:
<select name="myDropDown" onChange="somefunc()">
Is it possible to trap the onChange event outside of the select
statement? Something like this:
document.forms[0].myDropDown.onChange = somfunc;
That would assign a reference to somfunc to the _onchange_ (note
capitalisation) attribute of the select. When the selected option is
changed, somfunc is called.
Assigning an onchange (or any other 'on' event) effectively creates a
property of the element's DOM object, think of it like:
elObj = {
id : '',
name : 'myDropDown',
onchange : function(event){ somfunc(); },
onclick : function(event){ .... },
style : {
color: '...',
backgroundColor: '...'
},
/* ... lots more properties ... */
}
Where the script content of the HTML on* attribute is inserted as the
body of the anonymous function that is the value of the appropriate on*
property. The event parameter is automatically passed in the W3C model,
for IE you have to get it with window.event.
If that fits your definition of 'trap the onchange event' then yes, you
can do it.
However, if what you are trying to do is detect if the onchange event is
executed using some kind of global listener, no you can't. Whatever you
want to happen when the change event occurs must be called from the
onchange function.
Using shortcut property accessors as above is non-standard but widely
supported. It will cause somfunc to replace references to any functions
that might have been assigned to the onchange event previously. If you
want to add an extra function, say heyOnchangeFired(), then use
addEventListener (W3C event model) and attachEvent (IE event model).
Search the archives, there are many examples.
--
Rob