29

I have code like this at the bottom of my form.

<p>
  <input type="checkbox" id="agreed">
  I agree to keep my Username and Password confidential and uphold 
  the integrity of this pharmacy
</p>
<input type="submit" id="submit" disabled class="formbutton button-primary" value="Go">

I want this a listener in JavaScript to be able to enable the submit button. I know it might be wrong or something but my JavaScript looks sort of like this

function track() {
    if ( document.getElementById("agreed").checked==true ) {
        document.getElementById("submit").removeAttribute("disabled");
    } else {
        document.getElementById("agreed").checked==false
    }
};
Jacob
  • 77,566
  • 24
  • 149
  • 228
Gottfried Tetteh
  • 393
  • 1
  • 3
  • 5
  • make a js fiddle to show what you are trying to do – zgr024 Jul 29 '15 at 16:08
  • How about just call the script when you check the box? See http://stackoverflow.com/questions/3197702/html-checkbox-onclick-called-in-javascript – thllbrg Jul 29 '15 at 16:08
  • The dirty way... `` and your assignment to false needs one =, not 2... `document.getElementById("agreed").checked=false` – zgr024 Jul 29 '15 at 16:09
  • If it works then it isn't wrong, it only might be hard to maintain if you need to do a lot of checks. – html_programmer Jul 29 '15 at 16:15

2 Answers2

58

You can do this for the input:

<input type='checkbox' onchange='handleChange(this);'>Checkbox

And this for enabling the button:

function handleChange(checkbox) {
    if(checkbox.checked == true){
        document.getElementById("submit").removeAttribute("disabled");
    }else{
        document.getElementById("submit").setAttribute("disabled", "disabled");
   }
}
Kerem
  • 11,377
  • 5
  • 59
  • 58
Lvkz
  • 946
  • 14
  • 20
  • 1
    hey man, thanks for your answer. This is my first project with coding and all...im trying my hands out on this for a friend. Thanks anyways it did work. – Gottfried Tetteh Jul 29 '15 at 17:05
3

Try Below for your requirement using jQuery.

$('#checky').click(function(){
     
    if($(this).attr('checked') == false){
         $('#postme').attr("disabled","disabled");   
    }
    else {
        $('#postme').removeAttr('disabled');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<input type="checkbox" checked="checked" id="checky"><a href="#">I agree to keep my Username and Password confidential and uphold 
  the integrity of this pharmacy</a>
<br>
<input type="submit" id="postme" value="submit">

Using JavaScript, Please refer This

Community
  • 1
  • 1
Janty
  • 1,708
  • 2
  • 15
  • 29