Javascript array [] length is undefined when only single element
| From: | Daevid Vincent | Date: | Wed, 18 Feb 2004 01:21:29 +0000 |
| Subject: | Javascript array [] length is undefined when only single element | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-178044@lists.php.net to get a copy of this message | ||
Okay kids. Here's another strange one. Very simple form. The array is
dynamically generated from PHP as per the suggested method:
http://us2.php.net/manual/en/faq.html.php#faq.html.arrays
However, when there is only a single element, the myMultiSelect.length is
undefined in javascript.
If there are two or more elements, then the length works as expected...
---------------------- snip ----------------------
<SCRIPT>
function checkAudits()
{
var theForm = document.AddSchedule;
var myMultiSelect = theForm['RunVuln[]'];
// this doesn't work either.
//var a = document.forms[0].elements['RunVuln[]'].length;
//alert("a: " + a);
selectedAudits = 0;
for(i = 0; i < myMultiSelect.length; i++)
if (myMultiSelect[i].checked) selectedAudits++;
alert("selectedAudits: "+selectedAudits+" myMultiSelect.length:
"+myMultiSelect.length);
if (selectedAudits < 1)
{
alert("You must select at least one.");
return false;
}
return true;
}
</SCRIPT>
<FORM ACTION="/poop.php" METHOD=POST NAME="AddSchedule">
<input type=checkbox name="RunVuln[]" value="foo"> foo <BR>
<!-- uncomment this line and it works. with it commented, the length
is undefined. WTF?!
<input type=checkbox name="RunVuln[]" value="bar"> bar <BR>
-->
<INPUT TYPE=SUBMIT NAME=SUBMIT VALUE="Update Selections"
onClick='return checkAudits();'>
</FORM>