Please help with a form (javascript)

by 6 replies
7
Hi,

Here's the form:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Checklist</title>
<script type="text/javascript">
function disable_enable(){
if (document.all || document.getElementById){
if (document.reqcheck.checkit.disabled==true)
document.reqcheck.checkit.disabled=false
else
document.reqcheck.checkit.disabled=true
}
}
</script>
</head>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
Checklist:
<form action="#" method="post" name="reqcheck">
<ul>
<li>Check if _____ is done <input type="checkbox" name="reqcheck1" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck2" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck3" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck4" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck5" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck6" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck7" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck8" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck9" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheck0" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqchecka" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheckb" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheckc" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqcheckd" onclick="javascript:disable_enable()"></li>
<li>Check if _____ is done <input type="checkbox" name="reqchecke" onclick="javascript:disable_enable()"></li>
</ul>
<input name="checkit" type="submit" value="Submit" disabled="disabled">
</form>
</BODY>
</HTML>
I want this form to require all boxes be checked, before the submit button can be clicked...

What do I need to modify/add/whatever to get it to work?

The action after the submit button is clicked is not important...

Thanks, and...

Be Well!
ECS Dave
#programming #form #javascript
  • Try this one.
    replace the onclick="javascript:checkIt()"
    • [1] reply
    • I tried your code sonia2012, and it never seemed to allow the button to get enabled...

      Thank you for the effort though.

      While the code I posted was "funny", in that it allowed all odd instances of checkboxes being checked to enable the button, it did ultimately "enable the button".

      I'm no js coder, but was just trying fix up a little something...

      Thanks again...

      Be Well!
      ECS Dave
  • For the following to work, you'll need to put an ID on your submit button, i.e.:
    <input type="submit" id="checkit" name="checkit" value="Submit" />

    Code:
    <script type="text/javascript">
        function disable_enable(){
            var inputs = document.getElementsByTagName("input");
            var cbs = [];
            var checked = [];
            for (var i = 0; i < inputs.length; i++) {
              if (inputs[i].type == "checkbox") {
                cbs.push(inputs[i]);
                if (inputs[i].checked) {
                  checked.push(inputs[i]);
                }
              }
            }
            if ( cbs.length == checked.length ) {
                document.getElementById("checkit").disabled = false;
            }
        }
    </script>
    • [ 1 ] Thanks
    • [1] reply
    • Awesome...

      "Thanks" added to your total

      I appreciate your help, and the time you took to give it.

      Be Well!
      ECS Dave

      P.S. Being as I'm asking for more...
      What would it take to fix it so's if all boxes
      were to be checked, and any one were to get
      unchecked, for the button to be disabled again?
      Now for the "Being as I asked for more" - How much do I owe you?
      • [1] reply

Next Topics on Trending Feed