469,890 Members | 2,121 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Changing value of multiple elements with same name at once?

I am doing a form for my site and would like to enable and disable a radio
button set depending on whether one of the choices on an outer radio button
set is checked. How can I refer to all the inputs of the inner radio button
set (they all share a common name) with javascript. I tried
document.getElementsByNames('thename') but it doesn't work. I know this is
because this method returns an array which you must then refer to by a
specific index number, unless there is another way. I would like to refer to
all of them at once and then set their disabled status to false. Can someone
please help. Thanks.
Jul 20 '05 #1
6 3898
@SM
TheKeith a ecrit :
I am doing a form for my site and would like to enable and disable a radio
button set depending on whether one of the choices on an outer radio button
set is checked. How can I refer to all the inputs of the inner radio button
set (they all share a common name) with javascript. I tried
document.getElementsByNames('thename') but it doesn't work. I know this is
because this method returns an array which you must then refer to by a
specific index number, unless there is another way. I would like to refer to
all of them at once and then set their disabled status to false. Can someone
please help. Thanks.


To disable (or enable) any serie of radio button :

function disabl(radioname,myform,state){
for(var i=0;i<myform.length;i++)
if(myform.elements[i].name==radioname)
myform.elements[i].disabled=state;
}

<form>
<input type=radio name="foo"> Shoes <br>
<input type=radio name="foo"> Shirts <br>
<input type=radio name="truc"> Pencils <br>
<input type=radio name="foo"> Ties <br>
<input type=radio name="truc"> Papers <br>
<input type=radio name="articles"
onclick="if(this.checked==true) {
disabl('truc',this.form,'true');
disabl('foo',this.form,'false'); }
else {
disabl('truc',this.form,'false');
disabl('foo',this.form,'true'); }"> Clothes
<input type=radio name="articles"
onclick="if(this.checked==true) {
disabl('truc',this.form,'false');
disabl('foo',this.form,'true');}
else {
disabl('truc',this.form,'true');
disabl('foo',this.form,'false'); }"> Desk
</form>

If the 1st radio button "articles" is checked (selected)
make state of buttons "foo" enabled and buttons "truc" disabled
if 1st "articles" deselected : "foo" are disabled and "truc" enabled
That wiln't work with my NC4.5 who doesn't understand 'disabled'

--
************************************************** ************
Stéphane MORIAUX : mailto:st*********************@wanadoo.fr
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)
http://perso.wanadoo.fr/stephane.moriaux/internet/
************************************************** ************
Jul 20 '05 #2
"@SM" <st**************@wanadoo.fr> wrote in message
news:3F***************@wanadoo.fr...
<snip>
onclick="if(this.checked==true) {

<snip>

The "checked" attribute of a radio button is boolean, that means it can
only have one of the two values true of false. The - if - statement
needs to resolve its expression to a boolean value, and comparisons
always produce boolean results. So, to take a boolean value and compare
it with a boolean value to produce a boolean result is superfluous. In
an - if - statement is _never_ necessary to use the type converting (as
opposed to strict) comparison with a boolean value because the result
will always directly reflect the type-converted trueness of the variable
operand, and if that operand was boolean to start with there is even
less point:-

if(this.checked){
...
}

Richard.
Jul 20 '05 #3
@SM
Richard Cornford a ecrit :
<snip>
very interesting considerations
<snip>

if(this.checked){
...
}

Richard.


I will try it on my old browser to see what he thinks of that.

--
************************************************** ************
Stéphane MORIAUX

Jul 20 '05 #4

"@SM" <st**************@wanadoo.fr> wrote in message
news:3F***************@wanadoo.fr...
TheKeith a ecrit :
I am doing a form for my site and would like to enable and disable a radio button set depending on whether one of the choices on an outer radio button set is checked. How can I refer to all the inputs of the inner radio button set (they all share a common name) with javascript. I tried
document.getElementsByNames('thename') but it doesn't work. I know this is because this method returns an array which you must then refer to by a
specific index number, unless there is another way. I would like to refer to all of them at once and then set their disabled status to false. Can someone please help. Thanks.


To disable (or enable) any serie of radio button :

function disabl(radioname,myform,state){
for(var i=0;i<myform.length;i++)
if(myform.elements[i].name==radioname)
myform.elements[i].disabled=state;
}

Yeah I was just wondering if there was an easier way without having to use a
loop. I got it going with a loop too--I guess I'll just have to stick with
that. Thanks.
Jul 20 '05 #5
@SM
TheKeith a ecrit :
"@SM" <st**************@wanadoo.fr> wrote in message
news:3F***************@wanadoo.fr...

To disable (or enable) any serie of radio button :

function disabl(radioname,myform,state){
for(var i=0;i<myform.length;i++)
if(myform.elements[i].name==radioname)
myform.elements[i].disabled=state;
}


Yeah I was just wondering if there was an easier way without having to use a
loop. I got it going with a loop too--I guess I'll just have to stick with
that. Thanks.


Loop is not difficult and is instantly executed

In my mind you cannot evite the loop way because you have
to pass on each right element to tell it its state (dis/en abled)

You can use a DOM function (less heavy and faster) ==>
there is (with both : DOM and JS if DOM uninplemented)

function disabl(radioname,myform,state){
if(document.getElementsByName)
{
R = document.getElementsByName(radioname);
for(i=0;i < R.length;i++) R[i].disabled=state;
}
else
for(var i=0;i < myform.length;i++)
if(myform.elements[i].name==radioname)
myform.elements[i].disabled=state;
}

--
******************
Stéphane MORIAUX

Jul 20 '05 #6
Lee
@SM said:
Loop is not difficult and is instantly executed

In my mind you cannot evite the loop way because you have
to pass on each right element to tell it its state (dis/en abled)

You can use a DOM function (less heavy and faster) ==>
there is (with both : DOM and JS if DOM uninplemented)

function disabl(radioname,myform,state){
if(document.getElementsByName)
{
R = document.getElementsByName(radioname);
for(i=0;i < R.length;i++) R[i].disabled=state;
}
else
for(var i=0;i < myform.length;i++)
if(myform.elements[i].name==radioname)
myform.elements[i].disabled=state;
}


It's actually even more efficient than that, since elements
of a form that share a NAME attribute can be referred to
using array notation:

function disabl(radioname,myform,state){
for(var i=0;i<myform.elements[radioname].length;i++){
myform.elements[radioname][i].disabled=state;
}
}

Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Holden Caulfield | last post: by
4 posts views Thread by multimatum2 | last post: by
7 posts views Thread by Stefan Finzel | last post: by
6 posts views Thread by Thaynann | last post: by
4 posts views Thread by Siah | last post: by
4 posts views Thread by andychambers2002 | last post: by
2 posts views Thread by Gary Dale | last post: by
8 posts views Thread by mike_solomon | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.