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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval on checkbox

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
If I have a couple of checkboxes: checkbox1, checkbox2, checkbox3....

I would like to loop on it using an eval(document.form.checkbox i.checked)....where i is 1, 2 or 3...

is this possible?

Thanks a lot.
 
You could use the form.elements array, and ask each element for it's type. if(type=='checkbox')...do your checking.
b2 - benbiddington@surf4nix.com
 
Thanks b2, actually I'm using that technique right now and it works, the problem is performance, it goes through all of your elements and checks for checkboxes...it's really slow for it has to loop through everything, checkbox, input, even text and buttons...that's why I'm looking for a way to use the eval() but just can't master this eval() ...
do you know how to eval(checkbox i) where i could be a counter?

Thanks
 
Ahh - O.K. yes you should be able to.Here you go:

function selectAll(){
for(var i=1;i<5;i++){

eval(&quot;document.F1.checkbox&quot; + i+ &quot;.checked=true&quot;);
}

}


The eval() method executes strings as working code - so you just have to make sure you have astring as an argument. The integer i is converted automatically to a string - when they are concatenated.

P.S. make sure you have the right value in the for brackets - because if it iterates past the number of existing objects - you will get an error message, even though it will not affect anything else, just an annoyance.

Also another curious thing - i tried putting i=5, for the top limit, and the browser hangs (?) hmmm - so I guess you have to use less than(<) weird!
b2 - benbiddington@surf4nix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top