Solved

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

Posted on 2014-03-06
9
102 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

707 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

16 Experts available now in Live!

Get 1:1 Help Now