[Javascript] Javascript - radio buttons error

Abyss info at abyss.ws
Sun Mar 27 16:13:14 CST 2005


Wow..thank you....that is great!
your the first person (and only person) to explain about the DOM which 
really helped...
I really appreciate that help....

" As Netscape's original JavaScript spec helpfully states" do you know where 
i can get a copy of that spec?

Abyss
PS Thanks again...heaps

----- Original Message ----- 
From: "Andrew Clover" <and-babble at doxdesk.com>
To: <javascript at LaTech.edu>
Sent: Sunday, March 27, 2005 2:15 PM
Subject: Re: [Javascript] Javascript - radio buttons error


> > but for some reason it does not work if there is only 1 radio
> > button on the form...can anyone tell me why?
>
> Because the JS (DOM Level 0) Form interface is badly designed.
>
>   var f= document.getElementById('someform');
>   var el= f.elements.elname;
>
> If there is one field with name="elname", el will be an HTMLInputElement. 
> If there is more than one, el will be an Array of HTMLInputElements. As 
> Netscape's original JavaScript spec helpfully states:
>
>   You need to be aware of this situation in your code and know whether
>   myField refers to a single element or to an array of elements.
>
> One way of guessing which is the case is to check for the existence of a 
> 'length' attribute and assume that we have a single input if it's 
> undefined. This works as long as no-one starts adding 'length' properties 
> to form field objects. For example:
>
>   // Get list of field elements by name - always return Array/NodeList
>   //
>   function getFieldsByName(form, name) {
>     var el= form.elements[name];
>     if (!el) return new Array();
>     if (!el.length) return new Array(el);
>     return el;
>   }
>
> -- 
> Andrew Clover
> mailto:and at doxdesk.com
> http://www.doxdesk.com/
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 




More information about the Javascript mailing list