I have a problem and don´t know how to fix it, I have this code
var apply=document.getElementById("regulartextfields");
var counterrors=0;
if (apply.addEventListener)
{
apply.addEventListener("change", checkfield_1, false);
}
else if (apply.attachEvent)
{
apply.attachEvent("onchange", checkfield_1);
}
which works perfectly fine in both internet explorer and firefox for the following case
<input type="text" size="15" id="regulartextfields">
But only works in firefox in this case
<div id="regulartextfields">
<input type="text" size="15"/>
<input type="text" size="15"/>
<input type="text" size="15"/>
<input type="text" size="15"/>
</div>
In the last one firefox will attach an event to each field but it will stop working completely in IE, can someone tell me how I should go about attaching an event to multiple elements in IE?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
</head>
<body>
<div id="regulartextfields">
<input type="text" size="15"/>
<input type="text" size="15"/>
<input type="text" size="15"/>
<input type="text" size="15"/>
</div>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
function checkfield_1(){
alert('change');
}
function Init(){
var apply=document.getElementById("regulartextfields");
apply=apply.nodeName.toUpperCase()=='INPUT'?[apply]:apply.getElementsByTagName('INPUT');
var counterrors=0;
for (var z0=0;z0<apply.length;z0++){
if (apply[z0].addEventListener){
apply[z0].addEventListener("change", checkfield_1, false);
}
else if (apply[z0].attachEvent){
apply[z0].attachEvent("onchange", checkfield_1);
}
}
}
Init();
/*]]>*/
</script>
</body>
</html>
Thank you for your input, but since I am quite new to js I did not understand very well everything, could you please explain this line?
apply=apply.nodeName.toUpperCase()=='INPUT'?[apply]:apply.getElementsByTagName('INPUT');
thankyou:)
your require ment is for an individual INPUT or inputs nested in a DIV
so for an individual INPUT the line creats a arry of 1 INPUT
and for inputs nested in a DIV creats a arry of all nested INPUTs