[Javascript] Checking Radio Boxes with a loop

Nick Fitzsimons nick at nickfitz.co.uk
Mon Feb 16 10:47:10 CST 2009


Hi,

Unfortunately, that's actually the *worst* possible solution: eval()
incurs a terrible performance penalty, as it has to create (and then throw
away) a brand new instance of the script execution engine every time
through the loop.

It's generally agreed that "eval() is evil" [1] [2] although there are
some exceptions to this rule. Your case isn't one of those exceptions. I
would strongly suggest using the solution suggested by both David Dorward
and myself :-)

HTH,

Nick.

[1] <http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx> and
<http://blogs.msdn.com/ericlippert/archive/2003/11/04/53335.aspx> - Eric
Lippert worked on the Microsoft JScript engine.

[2]
<http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/165a4c861b08b6be/9e8f1bf40213ea6f?lnk=gst&q=eval+is+evil+shaver#9e8f1bf40213ea6f>
- Mike Shaver worked at Netscape and is now at Mozilla.

On Mon, February 16, 2009 3:29 pm, David Stoltz wrote:
> Thanks all! I see there are many solutions! This is the one I went with:
>
> 		for(i=1;i<34;i++)
> 		eval("form1.question" + i + "[0].checked=true");
>
> Again, thanks for all the quick responses!
>
>
> -----Original Message-----
> From: javascript-bounces at lists.evolt.org
> [mailto:javascript-bounces at lists.evolt.org] On Behalf Of Nick Fitzsimons
> Sent: Monday, February 16, 2009 9:08 AM
> To: JavaScript List
> Cc: javascript at lists.evolt.org
> Subject: Re: [Javascript] Checking Radio Boxes with a loop
>
> Hi,
>
> Try:
>
> for (i = 1; i < 33; i++) {
>     form1['question' + i][0].checked = true;
> }
>
> (I haven't tested this, but it should work.)
>
> Regards,
>
> Nick.
>
> On Mon, February 16, 2009 1:14 pm, David Stoltz wrote:
>> Hi all,
>>
>> I have 33 questions on a form, each with YES/NO possibilities. I'm
>> trying to add a button to "check all" yeses - Here's my code:
>>
>> <script>
>> function qyes(){
>>
>>                 for(i=1;i>33;i++)
>>                                 form1.question(i)[0].checked=true;
>>
>>
>> }
>> </script>
>>
>> I know this is not the right way to do this, and of course it doesn't
>> work....is there some easy way to dynamically say "question1", then
>> "question2", etc? If I replace the (i) with a number, it works of
>> course, and this is the part I'm having trouble with. I didn't want to
>> do this:
>>
>> form1.question1[0].checked=true;
>> form1.question2[0].checked=true;
>> form1.question3[0].checked=true;
>> etc....
>>
>>
>>
>> Any help?
>> Thanks!
>> _______________________________________________
>> Javascript mailing list
>> Javascript at lists.evolt.org
>> http://lists.evolt.org/mailman/listinfo/javascript
>>
>
>
> --
> Nick Fitzsimons
> http://www.nickfitz.co.uk/
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
>


-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/





More information about the Javascript mailing list