Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i disable a submit button when checkbox is uncheck?

I have something here that cannot seem to help me disable the submit button. any ideas?

<input type="checkbox" checked="checked" id="checky"><a href="#">terms and conditions</a>
<input type="submit" id="postme" value="submit">

$('#checky').click(function(){
    if($(this).checked == false){
         $('#postme').attr("disabled","disabled");   
    } else {
        $('#postme').removeAttr('disabled');
    }
});
like image 711
pivotal developer Avatar asked Mar 28 '11 11:03

pivotal developer


People also ask

How do you disable submit button until checkbox is checked?

checked) { // Disable button when checkbox is selected submitButton. disabled = true; } else { // Enable button when checkbox is deselected submitButton. disabled = false; } }); Below, you'll find some examples of different ways to solve the Disable Submit Button Until Checkbox Is Checked Javascript problem.

How do I make my submit button disabled?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

How do I disable the submit button when the field is empty?

Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.

How do you disable button on submit using JavaScript?

The simplest pure javascript solution is to simply disable the button: <form id="blah" action="foo. php" method="post" onSubmit="return checkForm();"> <button id="blahButton">Submit</button> </form> document.


2 Answers

You should be checking for the attribute checked and not the method.

Updated fiddle : http://jsfiddle.net/8YBu5/7/

$('#checky').click(function(){
    if($(this).attr('checked') == false){
         $('#postme').attr("disabled","disabled");   
    } else {
        $('#postme').removeAttr('disabled');
    }
});

EDIT

Remember that this code needs to be wrapped inside $(document).ready() or put at the bottom of your html code, otherwise your JS will bind itself to DOM elements that have not been loaded yet and will fail.

Wrapping it in .ready()

<script type='text/javascript'>
$(document).ready(function(){
    $('#checky').click(function(){
        if($(this).attr('checked') == false){
             $('#postme').attr("disabled","disabled");   
        } else {
            $('#postme').removeAttr('disabled');
        }
    });
});
</script>

This code can be put anywhere in the document and will only trigger once the DOM is ready so you don't have to worry about premature triggers.

Putting it at the bottom of your document

<!-- some HTML -->
<script type='text/javascript'>
        $('#checky').click(function(){
            if($(this).attr('checked') == false){
                 $('#postme').attr("disabled","disabled");   
            } else {
                $('#postme').removeAttr('disabled');
            }
        });
</script>
</body>
</html>

If you're putting it at the bottom of your document, you don't need to wrap it in .ready() because the javascript will not be read until everything else has loaded. There is a performance boost in this method (if you have a lot of JS)

You can use either one of these methods to make sure that your JS binds event handling methods to DOM elements only after they have finished loading.

like image 99
JohnP Avatar answered Sep 27 '22 22:09

JohnP


if(!$(this).is(':checked') ...
like image 24
manji Avatar answered Sep 27 '22 21:09

manji