Solved

Enable/Disable submit btn based on checkbox selection

Posted on 2007-03-28
2
1,171 Views
Last Modified: 2008-01-09
I have a form that is dynamically created by ColdFusion based on records in a DB. For each record returned in the form a checkbox is created next to it which allows the user to select the record for deletion via a submit button that executes a DELETE FROM SQL statement. I want to be able to enable and disable the submit/delete button based on whether or not any of the checkboxes are selected. One caveat is that the name of the checkbox(s) is the same no matter how many records are returned. I've tried the following JavaScript but it only works on the very last checkbox in the group.

<script type="text/javascript">
function checkBoxx(){
for (i=0; i<document.form.delID.length; i++)
{
if (document.form.delID[i].checked==true)
(document.form.delBTN.disabled=false);
else
document.form.delBTN.disabled=true;
}
}
</script>

Checkbox: <cfinput type="checkbox" name="delID" value="#preregid#" onClick="return checkBoxx();">

I understand why it's only working for the last checkbox but have no idea how to determine if any of the other checkboxes in the group preceding the last one are checked at any given time so that I may enable/disable the submit/delete button.
0
Comment
Question by:jriver12
2 Comments
 
LVL 5

Accepted Solution

by:
badrulnm earned 250 total points
ID: 18808370
The problem with your script is the last checkbox is always the factor whether the Delete button is disabled or not because of the if...else statement. If the last checkbox is checked, enable delete button, else, disable delete button.

The following would solved the problem:

<script type="text/javascript">
function checkBoxx(){
for (i=0; i<document.form.delID.length; i++)
{
document.form.delBTN.disabled=true;
  if (document.form.delID[i].checked==true)
  {
    document.form.delBTN.disabled=false;
    break;
  }
}
}
</script>
<form name="form">
Checkbox: <input type="checkbox" name="delID" value="#preregid#" onClick="return checkBoxx();">
Checkbox: <input type="checkbox" name="delID" value="#preregid#" onClick="return checkBoxx();">
Checkbox: <input type="checkbox" name="delID" value="#preregid#" onClick="return checkBoxx();">
<INPUT TYPE="button" name="delBTN" value="Delete" disabled>
</form>
0
 

Author Comment

by:jriver12
ID: 18808413
Thank you very much. The script works great. I had messed around with the break statement but couldn't figure out where to put it in the script.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

713 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