1

I was able to get this working using jquery 1.6, but I upgraded to 1.8 and can't get it to work. Here's the jfiddle: link

and the code:

HTML:

 <form id="ad_form" method="post" action="">
    <input type="checkbox" id="tosId" name="tos_name" style="padding:0; margin:0" ></input>       
    <br />
    <input type="submit" id="submitId" name="submit_name" value="Submit"  /></input>
 </form>​

jQuery:

 $('#submitId').attr('disabled', 'disabled');  

 function updateFormEnabled() 
     {
         if($('#tosId').prop('checked'))
         {
             $('#submitId').attr('disabled', '');
         } 
         else 
         {
             $('#submitId').attr('disabled', 'disabled');
         }
     }

 $('#tosId').change(updateFormEnabled);​
1
  • 1
    Try with prop rather than attr. Also i think we can remove disabled attribute if we want to enable, using removeAttr('disabled') instead of setting attr('disabled','') Commented Oct 19, 2012 at 13:20

6 Answers 6

3

It's a good idea to use prop method in jQuery 1.8:

function updateFormEnabled() 
{
    var checked = $('#tosId').prop('checked');
    $('#submitId').prop('disabled', !checked);
}

fiddle: http://jsfiddle.net/3aFaA/3/

Sign up to request clarification or add additional context in comments.

Comments

3

You should use .prop() to set disabled property

function updateFormEnabled(){         
    $('#submitId').prop('disabled', !$('#tosId').prop('checked'));        
 }

From jQuery docs http://api.jquery.com/prop/

The .prop() method should be used to set disabled and checked instead of the .attr() method.

Comments

1

use removeAttr()

  if($('#tosId').prop('checked'))
     {
         $('#submitId').removeAttr('disabled');
     } 
     else 
     {
         $('#submitId').attr('disabled', 'disabled');
     }

1 Comment

...you're the man. Thanks. Works perfect. I'll accept your answer "after 10 minutes"
0

should be $('#submitId').removeAttr('disabled');

instead of $('#submitId').attr('disabled', '');

Comments

0
$('#submitId').attr('disabled', 'disabled');  

function updateFormEnabled() 
{
    if($('#tosId').prop('checked'))
    {
        $('#submitId').attr('disabled', false);
    } 
    else 
    {
        $('#submitId').attr('disabled', true);
    }
}

$('#tosId').change(updateFormEnabled);​

should work jfiddle

Comments

0
$('#submitId').attr('disabled', 'disabled');  

function updateFormEnabled()
    {
        if($('#tosId').prop('checked'))
        {
            $('#submitId').attr('disabled', false);
        }
        else
        {
            $('#submitId').attr('disabled', true);
        }
    }

$('#tosId').change(updateFormEnabled);

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.