Solved

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

Posted on 2014-03-06
9
108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

696 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