Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Enable/Disable submit btn based on checkbox selection

Posted on 2007-03-28
2
Medium Priority
?
1,174 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

705 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