Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable /disable buttons in jquery?

My code looks like,

$("#OperationTypeId").change(function () {
var DropdownSelectionValue = $("#OperationTypeId :selected").val();
    alert(DropdownSelectionValue );
    if (DropdownSelectionValue == 3)
        {
            $("#Button1Id").attr('disabled');
            $(" Button2Id").attr('enable');;
            //$("#Button1Id").hide();
        }
        else {
            $("#Button1Id").attr('enable');
            $(" Button2Id").attr('disabled');;
        }
});

My code shows alert value correctly but not make the buttons enable/disable with condition of DropdownSelectionValue. As iam the beginner i dont know how to do this. Kindly tell me.

like image 547
Kavitha P. Avatar asked Jan 09 '23 19:01

Kavitha P.


2 Answers

From jQuery site

    Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. 

Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. 

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

To disable:

$('#Button1Id').prop('disabled', true);

To enable:

$('#Button2Id').prop('disabled', false);

http://api.jquery.com/prop/

like image 97
malkam Avatar answered Jan 15 '23 10:01

malkam


Try This :-

$("#OperationTypeId").change(function () {
   var DropdownSelectionValue = $("#OperationTypeId :selected").val();

   if (DropdownSelectionValue == 3) {
     $("#Button1Id").attr('disabled','disabled'); //or $("#Button1Id").prop('disabled',true);
     $("#Button2Id").removeAttr('disabled'); //Use '#' id selector
   }
   else {
     $("#Button1Id").removeAttr('disabled'); // or $("#Button1Id").prop('disabled',false);
     $("#Button2Id").attr('disabled','disabled'); //Use '#' id selector
   }
});
like image 32
Kartikeya Khosla Avatar answered Jan 15 '23 10:01

Kartikeya Khosla