31

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
    }
};
4
  • make a js fiddle to show what you are trying to do Commented Jul 29, 2015 at 16:08
  • How about just call the script when you check the box? See stackoverflow.com/questions/3197702/… Commented Jul 29, 2015 at 16:08
  • The dirty way... <input type="checkbox" id="agreed" onChange="track()"> and your assignment to false needs one =, not 2... document.getElementById("agreed").checked=false Commented Jul 29, 2015 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. Commented Jul 29, 2015 at 16:15

2 Answers 2

61

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");
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.