How to test if an element is a radio button

L

laredotornado

Hi,

If I am given an element name (e.g. the "name" attribute of a DOM
object), how can I test if that element is a radio button?

Thanks, - Dave
 
G

Gregor Kofler

Am 2010-04-23 22:43, laredotornado meinte:
Hi,

If I am given an element name (e.g. the "name" attribute of a DOM
object), how can I test if that element is a radio button?

With its type property.

Gregor
 
L

laredotornado

Am 2010-04-23 22:43, laredotornado meinte:



With its type property.

Gregor

--http://www.gregorkofler.com

I tried that, but it is not producing anything. On my page, I have a
single form, and this code

console.log("elt name:" + eltName + " type:" +
document.forms[0].elements[eltName].type);

produces:

elt name:functionSpace type:undefined

Here is the HTML ...

<div class="field-wrapper">
<label for="function-space-yes" class="lbl-function">
*Function space and /or meeting space needed?
</label><br />

<input id="function-space-yes" name="functionSpace" class="empty
lbl-function" type="radio" value="yes"/>
&nbsp;Yes
<input id="function-space-no" name="functionSpace" class="empty
lbl-function" type="radio" value="no"/>
&nbsp;No
</div>


- Dave


Any other thoughts? - Dave
 
T

Thomas 'PointedEars' Lahn

laredotornado said:
Gregor said:
laredotornado meinte:

With its type property.

I tried that, but it is not producing anything. On my page, I have a
single form, and this code

console.log("elt name:" + eltName + " type:" +
document.forms[0].elements[eltName].type);

produces:

elt name:functionSpace type:undefined

Here is the HTML ...

That is not interoperable HTML, the slashes do not belong there in HTML:
<div class="field-wrapper">
<label for="function-space-yes" class="lbl-function">
*Function space and /or meeting space needed?
</label><br /> ^^
<input id="function-space-yes" name="functionSpace" class="empty
lbl-function" type="radio" value="yes"/> ^
&nbsp;Yes
<input id="function-space-no" name="functionSpace" class="empty
lbl-function" type="radio" value="no"/>
^
(see also <http://validator.w3.org/>) -'

When two controls in the same form have the same name, as it must be the
case for each item of a radio button group, they are referred to by items of
a NodeList implementation. That object does not have a `type' property, but
the objects its items refer to have.

console.log("elt name:" + eltName + " type:" +
document.forms[0].elements[eltName][0].type);

This is a very old and very well-known issue. One must wonder if you have
ever RTFM, or have read the FAQ.

<http://jibbering.com/faq/#formControlAccess>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top