RE: [PHP] Javascript array [] length is undefined when only singl e element
| From: | Ford, Mike [LSS] | Date: | Wed, 18 Feb 2004 11:43:20 +0000 |
| Subject: | RE: [PHP] Javascript array [] length is undefined when only singl e element | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-178091@lists.php.net to get a copy of this message | ||
On 18 February 2004 01:21, Daevid Vincent contributed these pearls of wisdom:
> 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...
Correct. If there is only a single element with a name, JavaScript creates a single scalar checkbox
property with that name; if there are multiple elements with the same name, it creates an array of
those elements as that property. The existence of the '[]' in the element name has no
significance for JavaScript -- it goes solely on how many of the elements there are.
Checking whether the .length property exists is one way of telling whether you have a single element
or multiple ones!
Cheers!
Mike
--
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211