Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Form elements 1

Status
Not open for further replies.

gbaughma

IS-IT--Management
Staff member
Joined
Nov 21, 2003
Messages
4,773
Location
US
OK, here's my "dilemma dujour"....

I have loaded an array with names of checkboxes on a form. I want to see if ANY of these checkboxes are checked. i.e.

var aryTest = new Array("C1","C2","C3")
var flgChecked = 0

for(i=0;i<=aryTest;i++){
// the line I'm having a problem with.... something like
if(document.forms['form1'].elements[aryTest].checked==true){
... etc...

... obviously, I can't nest the [['s together. I've verified that the NAMES of the checkboxes are in there.... my form is simply:

<FORM name="form1">
<input type="checkbox" name="C1">bla bla<br>
<input type="checkbox" name="C2">bla bla<br>

... etc.

Any thoughts on this one?

Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Give the checkboxes ID tags and use those instead then use this.
Code:
for(i=0;i<aryTest.length;i++){
  if(document.getElementById('"'+aryTest[i]+'"').checked){
    alert(aryTest[i] + " is checked"); 
  }

getElementById is a DOM method that is widely supported, you just have to have an ID tag in your element to go after.

For your for loop you need aryTest.length to get the total number of elements in the array. And you only need < rather than <= because the array starts at 0 so an array with 10 elements would be 0-9 while the value of aryTest.lenght is 10. So you go 0 to <10.


It's hard to think outside the box when I'm trapped in a cubicle.
 
That almost worked... I had to take out the quotes... thank you!

Code:
// Set a flag
var flgRestraint = 0;

	for(i=0;i<aryRestraint.length;i++){
		if(document.getElementById(aryRestraint[i]).checked){
		flgRestraint=1;
		}

	}


Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Wonder why I thought you needed the quotes. Hmm.
Thanks for the star you just put me at 100. :)



It's hard to think outside the box when I'm trapped in a cubicle.
 
... at least I remember to give them. :) I wish people would remember to give them to ME when they say "thank you" for my posts.... hehe



Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Know what you mean. I went days without getting a star when I was hovering at 97/98 and just waiting to break that 100 mark even though I put in a lot of effort helping with successful results.

Most newbies just do not understand the system and will eventually learn. Sometimes they know about stars but when several people chime in with help they do not want to choose one person over the others so either give no stars or give them out freely to all.

I'm no star whore but it is nice to get recognition.


It's hard to think outside the box when I'm trapped in a cubicle.
 
I'm no star whore but it is nice to get recognition.

I am. Hehe



Just my 2¢

"In order to start solving a problem, one must first identify its owner." --Me
--Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top