Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have looked on here and tried every answer available, but it is still not working for me. I am trying to disable the submit button if checkbox is not checked. Here is my jquery:

var userTerms = $('input#agree');
     if(userTerms.is(':checked')) {
        $("#aggtxt").addClass("err");
        $("#aggtxt").removeClass("error");
        $("#aggtxt").text(" Agreed");
        $("#bookit").attr('disabled', false);
        }else{
        $("#aggtxt").removeClass("err");
        $("#aggtxt").addClass("error");
        $("#aggtxt").text(" Please check terms and conditions box");
        $("#bookit").attr('disabled', true);
        }

         if ($('#bookit').is(':disabled') == true) { 
            $("#booktxt").removeClass("err");
            $("#booktxt").addClass("error");
            $("#booktxt").text(" Before you can submit this form please check the errors in form marked in red.");
            }else{
                $("#booktxt").addClass("err");
                $("#booktxt").removeClass("error");
                $("#booktxt").text(" Your information looks good, continue to booking...");
                }   
});

and part of my HTML form:

<p><span id="aggtxt"></span><br /><input type="checkbox" name="agree" id="agree" />  I agree to <a href="http://travel.ian.com/index.jsp?pageName=userAgreement&locale=en_US&cid=379849" target="_blank">Terms and Conditions</a> and understand Cancellation Policy.<p>
<span id="booktxt"></span>
<input type="submit" name="bookit" value="" class="bookit" id="bookit" />
share|improve this question
add comment (requires an account with 50 reputation)

3 Answers

My take on this:

http://jsfiddle.net/bluz/peans/

Hope this helps :) Paz.

share|improve this answer
u havent used "Var" to declare a variable in ur script this is the correct one jsfiddle.net/peans/39 – Vaibhav Bhanushali Apr 28 at 17:24
add comment (requires an account with 50 reputation)

Try this:

if($('#agree').val() == 'true')

More info on jQuery documentation.

Correction:

is(':checked') method only return when a radio/check box is checked.

share|improve this answer
nope.. #bookit is the submit button – liveandream Apr 13 '12 at 1:17
1  
@liveandream I corrected it now, you should test whether the checkbox is checked or not . – SIFE Apr 13 '12 at 1:20
add comment (requires an account with 50 reputation)

Try this:

$("#bookit").attr('disabled', 'disabled');

I tried this:

<input type="checkbox" name="agree" id="agree" onclick="$('#bookit').attr('disabled', !$(this).is(':checked'));" />
    I agree to <a href="http://travel.ian.com/index.jsp?pageName=userAgreement&locale=en_US&cid=379849" target="_blank">Terms and Conditions</a> and understand Cancellation Policy.<p>

And it disables the button if you uncheck the checkbox.

share|improve this answer
The former is acceptable as well and is not an issue. – Ohgodwhy Apr 13 '12 at 1:12
New hotness!! $el.prop('disabled', true) – elclanrs Apr 13 '12 at 1:15
nope :( did not work – liveandream Apr 13 '12 at 1:18
the disabled button code works fine, cause I also have it with other parts of the form.. it is only failing when I check if check box is checked. – liveandream Apr 13 '12 at 1:18
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.