In the following example:
<html>
<head>
<SCRIPT language="JavaScript">
function checkcheckboxElem(checkboxElem)
{
alert ("checkboxElem " + checkboxElem.length);
}
</script>
</head>
<body>
<form name="checkForm">
Master: <input type="checkbox" name="master" value="6694" onclick="checkcheckboxElem(document.checkForm.checkboxElem)"><br>
Child1: <input type="checkbox" name="checkboxElem" value="2">
</form>
</body>
The alert outputs ‘undefined’. For some reason it does this if there is only one checkbox. If i add another checkbox then it outputs the correct length. If i change it as shown below then it outputs 2
<html>
<head>
<SCRIPT language="JavaScript">
function checkcheckboxElem(checkboxElem)
{
alert ("checkboxElem " + checkboxElem.length);
}
</script>
</head>
<body>
<form name="checkForm">
Master: <input type="checkbox" name="master" value="6694" onclick="checkcheckboxElem(document.checkForm.checkboxElem)"><br>
Child1: <input type="checkbox" name="checkboxElem" value="2">
Child2: <input type="checkbox" name="checkboxElem" value="1">
</form>
Why does it return ‘undefined’ if it is only one checkbox?
Thanks
In the first case
document.checkForm.checkboxElemrefers to a single input element, in the second case it refers to an array of input elements. You could do: