Solved

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

Posted on 2014-03-06
9
103 Views
Last Modified: 2014-03-07
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!
0
Comment
Question by:brucegust
  • 4
  • 3
  • 2
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39910118
<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!");
 }
0
 

Author Comment

by:brucegust
ID: 39910131
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?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39910150
It's because you've random reference that you should use this
0
 
LVL 9

Accepted Solution

by:
Sar1973 earned 350 total points
ID: 39911902
You should use the eval function if you want to declare a variable assigning to it an index:
eval("var form.deleteCapX"+row+".checked;");
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 150 total points
ID: 39912142
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
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39912177
Needlessy, it says...however, how can you declare a variable in a loop in which it depends on its index?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39912238
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 :))
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39912277
I refer on my previous answer (see also http://www.w3schools.com/jsref/jsref_eval.ASP).
0
 

Author Comment

by:brucegust
ID: 39912576
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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now