Solved

Enable/Disable submit btn based on checkbox selection

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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

825 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