Clarence Gardner wrote:
It appears that events are not generated while an event handler is running.
Can this be true?
It has nothing to do with whether the event handler is running or not.
Javascript may or may not fire events for things that happen programmatically.
Changing the value of an input programmatically does not trigger the onchange
event, issuing document.forms['formName'].submit() does not fire the onsubmit
event of the form (the exception being some versions of Opera, which _did_
incorrectly fire the onsubmit event of the form when calling it's submit()
method).
However, calling the click() method of a button causes that button's onclick
event to fire, and calling the focus() and blur() methods on an element both
cause the onfocus and onblur events to fire respectively.
Anyway, if you want to process the onchange event of an input after changing it's
value programmatically, the correct way to do it is as follows:
<script type="text/javascript">
function test(f) {
var theElement = f.elements['myInput'];
if (theElement) {
theElement.value = 123;
if (theElement.onchange) {
theElement.onchange();
}
}
}
</script>
<form name="myForm">
<input type="text" name="myInput" value="456"
onchange="alert('myInput=' + (+this.value +1));">
<input type="button" name="myButton" value="Test" onclick="test(this.form);">
</form>
Calling the onclick() method programmatically after changing the value
programmatically will have a much better chance of success then flipping the
focus around in an attempt to make the user agent trigger the event itself.
Of course, if you're already _in_ the onchange event when you do this, it would
be a good idea to include some way of preventing an endless loop, where the
onchange event changes the value, then calls the onchange event, which calls the
onchange event, etc.
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ -
http://jibbering.com/faq