473,386 Members | 1,819 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,386 software developers and data experts.

Help with loop/naming elements

<disclaimer>New to js.</disclaimer>

I have several pages, each with menues comprising checkboxes or radio
boxes within the same form. I presently 'brute force' clear the buttons
with individual scripts for each form, e.g.:

function clearLa() {
window.document.form1.button1La.checked=false;
window.document.form1.button2La.checked=false;
window.document.form1.button3La.checked=false;
window.document.form1.button4La.checked=false;
window.document.form1.button5La.checked=false;
}

Another set of buttons in the same form might be call, e.g., button1Ra et
seq.

I tried to simplify the above with a simple loop:

var counter = 1;
function clearLa() {
while (counter < 6) {
window.document.form1.button[counter]La.checked=false;
counter++;
}
}

But it seems to go into an infinite loop. Where am I going wrong?

I would ultimately like to have a single generic routine that I can pass
the number of buttons in the menu as well as the name of the button set
(e.g., buttonLa, buttonXz, etc. So, for a set of 8 buttons called
buttons1xy, button2xy...button8xy, I can write something like
'onClick = "clearButtons(xy,8).' How do I do it?

--
Ed Jay (remove M to respond by email)
Dec 4 '05 #1
6 1652
Well, try ->
function toggleAll(el, state) {
for (i=0;i<el.length;i++)
el[i].checked=state;
}

-----------------------------------------------------------

<form name="SOMETHING">

<input type="radio" name="DUCKS" value="1">
<input type="radio" name="DUCKS" value="2">
<input type="radio" name="DUCKS" value="3">
<input type="radio" name="DUCKS" value="4">
<input type="checkbox" name="DOVES" value="1">
<input type="checkbox" name="DOVES" value="2">
<input type="checkbox" name="DOVES" value="3">
<input type="checkbox" name="DOVES" value="4">
.....

<input type="button" onclick="toggleAll(this.form.DUCKS,
false)" value="unchek all ducks">
<input type="button" onclick="toggleAll(this.form.DUCKS,
true)" value="chek all ducks">
<input type="button"
onclick="toggleAll(this.form.DOVES, false)" value="unchek all doves">
<input type="button"
onclick="toggleAll(this.form.DOVES, true)" value="chek all doves">

</form>
Danny
Dec 4 '05 #2
Lee <RE**************@cox.net> wrote:
Ed Jay said:

<disclaimer>New to js.</disclaimer>

I have several pages, each with menues comprising checkboxes or radio
boxes within the same form. I presently 'brute force' clear the buttons
with individual scripts for each form, e.g.:

function clearLa() {
window.document.form1.button1La.checked=false;
window.document.form1.button2La.checked=false;
window.document.form1.button3La.checked=false;
window.document.form1.button4La.checked=false;
window.document.form1.button5La.checked=false;
}

Another set of buttons in the same form might be call, e.g., button1Ra et
seq.

I tried to simplify the above with a simple loop:

var counter = 1;
function clearLa() {
while (counter < 6) {
window.document.form1.button[counter]La.checked=false;
counter++;
}
}

But it seems to go into an infinite loop. Where am I going wrong?

I would ultimately like to have a single generic routine that I can pass
the number of buttons in the menu as well as the name of the button set
(e.g., buttonLa, buttonXz, etc. So, for a set of 8 buttons called
buttons1xy, button2xy...button8xy, I can write something like
'onClick = "clearButtons(xy,8).' How do I do it?
I don't believe your code goes into an infinite loop, but it's not
going to do what you want, either.
Although it's not causing problems, you really should initialize your
"counter" variable inside the function.
The important point you've missed is that when referring to form
elements using square-bracket notation, the bracket must contain
the entire name of the element, not just the variable part:

document.form1.elements["button"+counter+"La"].checked=false;

Thanks. As you can see from the header, I suspected as much.
function clearButtons(suffix,count) {
while (count) {
document.form1.elements["button"+(count--)+suffix].checked=false;
}
}

called as:

onclick = "clearButtons('La',8)"
This appears to be exactly what I'm trying to do. Thanks.

There are alternatives, such as naming all 8 buttons "buttonLa"
and allowing the browser to build the array for you:

function clearButtons(suffix) {
for(var i=0;i<document.form1.elements["buttons"+suffix].count) {
document.form1.elements["button"+suffix][i].checked=false;
}
}


--
Ed Jay (remove M to respond by email)
Dec 4 '05 #3
Danny <da*******@bluebottle.com> wrote:
Well, try ->
function toggleAll(el, state) {
for (i=0;i<el.length;i++)
el[i].checked=state;
}

-----------------------------------------------------------

<form name="SOMETHING">

<input type="radio" name="DUCKS" value="1">
<input type="radio" name="DUCKS" value="2">
<input type="radio" name="DUCKS" value="3">
<input type="radio" name="DUCKS" value="4">
<input type="checkbox" name="DOVES" value="1">
<input type="checkbox" name="DOVES" value="2">
<input type="checkbox" name="DOVES" value="3">
<input type="checkbox" name="DOVES" value="4">
.....

<input type="button" onclick="toggleAll(this.form.DUCKS,
false)" value="unchek all ducks">
<input type="button" onclick="toggleAll(this.form.DUCKS,
true)" value="chek all ducks">
<input type="button"
onclick="toggleAll(this.form.DOVES, false)" value="unchek all doves">
<input type="button"
onclick="toggleAll(this.form.DOVES, true)" value="chek all doves">

</form>

Thanks, Danny. This is simple and elegant.

--
Ed Jay (remove M to respond by email)
Dec 4 '05 #4
Ed Jay <ed***@aes-intl.com> wrote:
<disclaimer>New to js.</disclaimer>

I've progressed to a state of frustration.

I select one of 6 radio buttons. Each has the same name, but different
values. When a button is selected, an onClick function brings up another
menu. I want the second menu to use a string returned from the js
function.

The js:

var s= [
'uniform Thermal Pattern',
'regular thermovascular pattern',
'thermovascular network',
'irregular thermovascular pattern',
'distorted thermovascular pattern',
'anarchic thermovascular pattern'
]

function checkRadio(field) {
for(var i=0; i < field.length; i++)
if(field[i].checked)
alert("Alert in loop: " + s[i]);
document.write(s[i]);
return s[i];
}

I want to use the returned string as part of a dynamic sentence. E.g,

'The
<script type="text/javascript>
checkRadio(buttonName);document.write(s[i])
</script>
and more text.

I've also tried document.write(checkRadio(buttonName);

I'm executing the script up to "return s[i];" If I put an alert after the
function call, I get the alert.

Besides trying to write in a language I don't understand, where have I
gone wrong in the above?

TIA,

--
Ed Jay (remove M to respond by email)
Dec 7 '05 #5
Ed Jay wrote:
Ed Jay <ed***@aes-intl.com> wrote:

<disclaimer>New to js.</disclaimer>
I've progressed to a state of frustration.

I select one of 6 radio buttons. Each has the same name, but different
values. When a button is selected, an onClick function brings up another
menu. I want the second menu to use a string returned from the js
function.

The js:

var s= [
'uniform Thermal Pattern',
'regular thermovascular pattern',
'thermovascular network',
'irregular thermovascular pattern',
'distorted thermovascular pattern',
'anarchic thermovascular pattern'
]

function checkRadio(field) {
for(var i=0; i < field.length; i++)
if(field[i].checked)
alert("Alert in loop: " + s[i]);
document.write(s[i]);
return s[i];
}

I want to use the returned string as part of a dynamic sentence. E.g,

'The
<script type="text/javascript>
checkRadio(buttonName);document.write(s[i])


Your checkRadio function returns the index, so the call to checkRadio
replaces the index 'i':

document.write(s[checkRadio(buttonName)]);

</script>
and more text.

I've also tried document.write(checkRadio(buttonName);


Close, but no cigar... you forgt the 's'. Alternatively, have
checkRadio return s[i], then the above line will work provided you fix
the syntax error.
document.write(checkRadio(buttonName));
// note additional closing bracket---^
[...]
--
Rob
Dec 7 '05 #6
RobG <rg***@iinet.net.au> wrote:
Ed Jay wrote:
Ed Jay <ed***@aes-intl.com> wrote:

<disclaimer>New to js.</disclaimer>
I've progressed to a state of frustration.

I select one of 6 radio buttons. Each has the same name, but different
values. When a button is selected, an onClick function brings up another
menu. I want the second menu to use a string returned from the js
function.

The js:

var s= [
'uniform Thermal Pattern',
'regular thermovascular pattern',
'thermovascular network',
'irregular thermovascular pattern',
'distorted thermovascular pattern',
'anarchic thermovascular pattern'
]

function checkRadio(field) {
for(var i=0; i < field.length; i++)
if(field[i].checked)
alert("Alert in loop: " + s[i]);
document.write(s[i]);
return s[i];
}

I want to use the returned string as part of a dynamic sentence. E.g,

'The
<script type="text/javascript>
checkRadio(buttonName);document.write(s[i])


Your checkRadio function returns the index, so the call to checkRadio
replaces the index 'i':

I edited my calling code to that as shown, above. I changed the Return
statement from 'return s[i];' to 'return 'i;' It still doesn't work.
</script>
and more text.

I've also tried document.write(checkRadio(buttonName);
Close, but no cigar... you forgt the 's'. Alternatively, have
checkRadio return s[i], then the above line will work provided you fix
the syntax error.


How?
document.write(checkRadio(buttonName));
// note additional closing bracket---^

For troubleshootong purposes, I rewrote radioCheck() as

function checkRadio(field) {
for(var i=0; i < field.length; i++)
if(field[i].checked)
document.write(s[i]);
document.write(i);
return i;
}

When called inline with:

<script type="text/javascript>
document.write(s[checkRadio(buttonName)]);
alert("Made it");
</script>

A new otherwise blank window appears with the correctly indexed string
followed by the index, 'i.' I do not get the alert. More interesting (to
me), when called, the function prints the correctly indexed string, but
the index always shows as 1 more than the array length. This tells me that
the loop progresses until it finds the checked radio button, prints s[i],
and continues to count through all values of i until it reaches the limit.

Playing around, I changed the loop to read:

function checkRadio(field) {
for(var i=0; i < field.length; i++)
if(field[i].checked) {ppp = i;
document.write(s[i]);
document.write(i);
document.write(ppp);
return i;
}
}

I still don't get the returned value, but the function at least shows the
correct index value and correct ppp. By this I mean that if I select
Button 3, the new window shows s[i] = s[3] and both i and ppp are written
as 3.

Regardless, I don't seem to be getting past the 'return i;' statement as
the alert following the code call isn't triggered.

--
Ed Jay (remove M to respond by email)
Dec 7 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven D'Aprano | last post by:
Are there any useful naming conventions for modules, classes and functions? For instance, should I name functions as verbs and classes as nouns? eg class Transformer(): pass def...
3
by: addi | last post by:
All, I will be eternally greatful if someone can provide snippet of code, URL or reference material that shows how to display data in a "n colums * n rows" format. I am new to ASP and have...
1
by: GaryM | last post by:
I hope someone can help me with this, I've been ploughing through the CSS 2 specs for a while trying to figure this out. Anyway... I am designing an index page for a website that has information...
5
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
8
by: Daz | last post by:
Hi all! This question may hopefully spark a little debate, but my problem is this: I am sure it's not just me who struggles to think up names for variables. Obviously, thinking up a name...
5
by: Ed Jay | last post by:
I have a switch statement that controls which of several containers is displayed or not. It currently looks like: function showHelp(n) { show('vhelp'); //makes parent container visible switch...
30
by: carlos123 | last post by:
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on. 1. Reading a file. 2. Taking that data read from the file and putting it into...
1
by: Greg Herlihy | last post by:
On Jun 12, 7:36 am, James Kanze <james.ka...@gmail.comwrote: I think that "CharacterSetBase" and "CharacterSet" (for the base and derived classes respectively) would sound more natural and...
1
by: tesker | last post by:
I'm working on a website that looks great in Firefox, IE 7 and IE 8 , but in IE 6 one of the elements appears below another element instead of to the right where it is supposed to be. The site is...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.