473,378 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 4060
@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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Holden Caulfield | last post by:
Hi there, for IE 5+ I need to be able to change certain TD tags´ background colors, and can´t figure out how to do it... For instance, in a table with 25 cells, somewhere between 5 or 10 will...
4
by: multimatum2 | last post by:
Hello, I need to enable/disable input text forms... But... I need to have the same style (color...) in both modes.. Could you help me ? Thanx a lot A small sample... ...
7
by: Stefan Finzel | last post by:
Hi, is there a way to change the display property on Windows Mobile 2003 SE Mobile/Pocket Internet Explorer? See following example. Please note: visibilty property has the same problem. Is...
6
by: Thaynann | last post by:
I have an app that i am developing, it access a website via HttpWebRequest and HttpWebResponse classes (eg http://thaynann.com/images/) and at the moment i am able to save all the image files from...
4
by: Siah | last post by:
Hi, I wanted to dynamically update pieces of my page in the following fashion. Look at the following html code: <span id='person_23_name'> Mike </span> <span id='person_28_name'> Siah </span>...
13
by: amykimber | last post by:
Hi all, I know I'm doign something really daft, but I can't get this to work... I have a form with a bunch of inputs called ship_owner - why the ? Because I'm submitting this page though php...
4
by: andychambers2002 | last post by:
I'm working on a "TempFile" class that stores the data in memory until it gets larger than a specified threshold (as per PEP 42). Whilst trying to implement it, I've come across some strange...
2
by: Gary Dale | last post by:
I have a form with a pull-down list with six options, each of which has a value set. The value is the e-mail account name (without the domain) of a group while the displayed value is the full name...
8
by: mike_solomon | last post by:
I have a button <input type="submit" name="Delete" value="Delete"> This code can not be changed I want to use Javascript to change the type I tried:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.