How do I check to see if this box is checked in Javascript?

Bruce Gust
Bruce Gust used Ask the Experts™
on
Here's my HTML:

<input type='Checkbox' name='deleteCapX3' value='226' 
                    onclick='delete_visibility(dfm,"3","3");'>

Open in new window


Here's my function:

function delete_visibility (form, row, count) {
 var var_ticker=0;
 var numRows = form.allocatedlistcount.value;
 if(form.deleteCapX'+row+'.checked)
 {
     alert("Yeah!");
 }

Open in new window


If I write my code like this:

if(form.deleteCapX3.checked)...

...it fires fine.

But if I want to grab the row number that's been passed into the function ("row), how do I write that? When I try form.deleteCapX+row+ I get an error and I can't figure out how to write it.

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
<input type='Checkbox' name='deleteCapX3' value='226'
                    onclick='delete_visibility(this,dfm,"3","3");'>

function delete_visibility (ref,form, row, count) {
 var var_ticker=0;
 var numRows = form.allocatedlistcount.value;
 if(ref.checked)
 {
     alert("Yeah!");
 }
Bruce GustPHP Developer

Author

Commented:
Leak, your suggestion is spot on, but I've got to code it like I've got it because I'm ultimately running everything through a loop, so I've got to figure out how to tack on to that form.DeleteCapx variable a number. I've tried the following:

form.Delete.Capx'+row+'.checked
form.Delete.Capx"+row+".checked

...and several other combos and nothing's working.

Any suggestions along those lines?
leakim971Multitechnician
Top Expert 2014

Commented:
It's because you've random reference that you should use this
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
You should use the eval function if you want to declare a variable assigning to it an index:
eval("var form.deleteCapX"+row+".checked;");
leakim971Multitechnician
Top Expert 2014
Commented:
You should use the eval function if you want to declare a variable assigning to it an index

don't do that!!!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Don.27t_use_eval.21

Commented:
Needlessy, it says...however, how can you declare a variable in a loop in which it depends on its index?
leakim971Multitechnician
Top Expert 2014

Commented:
however, how can you declare a variable in a loop in which it depends on its index?

@Sar1973 : Create a new question, I will be happy to answer it :))

Commented:
I refer on my previous answer (see also http://www.w3schools.com/jsref/jsref_eval.ASP).
Bruce GustPHP Developer

Author

Commented:
Thanks guys! As always the insight and even the professional sparring is appreciated in the way it brings to light the answer needed in order to serve the client.

Here's what worked:

 for( var rowIndex = 1; rowIndex <= numRows; rowIndex++ ) {
 eval('deletecb = form.deleteCapX'+rowIndex+'.checked;');

Granted, it's an eval which, according to some, is cancer on wheels, but it works and is consistent with the conventions that are being used on the project I'm working on. So, rock on, boys, rock on!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial