Misam
Misam

Reputation: 4389

Enabling a button based on condition with jQuery

I have a "Save Settings" button which is of type 'image'. I want it to be enabled only after a checkbox is checked by the user I m using..

$('#btnSaveProfile').attr("disabled",true);    
$('#btnSaveProfile').click(function(){
            if ($("#rdAccept").is(':checked')) 
            {
                $('#btnSaveProfile').attr("disabled",false);
                updateProfile();// calling a function here that saves data.
            }
        });

This does not work, any inputs....please

Upvotes: 0

Views: 1851

Answers (3)

redsquare
redsquare

Reputation: 78667

You are not using the checkbox click event, try the following

var $btn = $('#btnSaveProfile').attr("disabled",true);   

$('#rdAccept').click(function(){
   if (this.checked) {
       $btn.removeAttr("disabled");
   }
});

$btn.click(updateProfile);

Upvotes: 1

a'r
a'r

Reputation: 36999

You are disabling the element with id btnSaveProfile and then attaching a click handler, which will never get run because its disabled. You need to add a click handler to your checkbox, which will re-enable the save button.

Upvotes: 2

sharpner
sharpner

Reputation: 3937

$('#btnSaveProfile').attr("disabled",''); enables the button

$('#btnSaveProfile').attr("disabled",'disabled'); disables the button

Upvotes: 1

Related Questions