Click to See Complete Forum and Search --> : Is there such a thing as a global onchange event for a form?
bclem
09-01-2004, 08:26 AM
I know that you can add onchange="" to each of your form fields, but in one of my applications I have over 500+ form fields and that would take forever. Is there a way to just have one onchange event that catches every form fields?
For instance, does <form onchange="dosomething.function()"> work?
Thanks,
Brent
sciguyryan
09-01-2004, 08:57 AM
No, you can't the form tag does not have that event handler available.
But, you will be ablw to loop through all the elements in a form - this would act as a sort of global event (Something like):
function Loop(){
var Elms = document.getElementByTagName("input");
for (var i = 0; i < Elms.length; i++){
Elms[i].onchange = What ever you want to do here;
}
}
RyanJ
bclem
09-01-2004, 09:04 AM
Cool that looks like what I need, but one more question, where would I actually put the loop function? body onload?
Thanks,
Brent
gil davis
09-01-2004, 09:19 AM
No, there is no universal onchange for a form.
You should consider breaking up the form or simplifying it. If I were presented with a form that had 500 elements for me to fill out, I would leave.
You could loop through the form elements onload and add the onchange event.
<head>
<script>
function init() {
for (var i=0; i<document.forms.length; i++)
{if (document.forms[i].length)
{for (var j=0; j<document.forms[i].length; j++)
{document.forms[i][j].onchange = function () {alert("changed");}}
}
}
}
</script>
</head>
<body onload="init()">
<form onsubmit="return false">
<input type="button" value=" Hi there "><br>
<input type="text"><br>
<input type="radio"><br>
<input type="checkbox"><br>
<select>
<option>one
<option>two
<option>three
</select>
</form>
</body>