Entry
Can I call the onchange handler of a form element?
Can I call the onchange handler of a form element?
Feb 24th, 2000 18:48
Martin Honnen,
Yes you can call
document.formName.elementName.onchange()
First make a check it exists
if (document.formName.elementName.onchange)
document.formName.elementName.onchange();
Here is an example:
<HTML>
<HEAD>
<SCRIPT>
function calcTotal (field, price) {
var total = document.aForm.total;
total.value = parseFloat(total.value) + field.value * price;
}
function initTotal () {
for (var i = 0; i < document.aForm.elements.length; i++)
if (document.aForm.elements[i].onchange)
document.aForm.elements[i].onchange();
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="initTotal()">
<FORM NAME="aForm">
JavaScript Bibles:
<INPUT TYPE="text" NAME="quantityJB" VALUE="1" SIZE="4"
ONCHANGE="calcTotal(this, 19.99)">
<BR>
Kibology Handbooks:
<INPUT TYPE="text" NAME="quantityKH" VALUE="2" SIZE="4"
ONCHANGE="calcTotal(this, 4.99)">
<BR>
Total
<INPUT TYPE="text" NAME="total" VALUE="0" SIZE="8">
</FORM>
</BODY>
</HTML>