Solved

Enable/Disable submit btn based on checkbox selection

Posted on 2007-03-28
2
1,168 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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

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

763 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

8 Experts available now in Live!

Get 1:1 Help Now