Solved

Enable/Disable submit btn based on checkbox selection

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery Ajax - on change event not being picked up 9 64
Check input text, Number 7 55
Where is this content coming from? 4 40
Javascript Error 10 24
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

752 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