Gaz Winter
Gaz Winter

Reputation: 2989

Jquery to disable a HTML button not working

I am trying to get some jQuery to disable the confirm button on my form if the dropdown list is a certain value, but it doesnt seem to be working.

I have read lots of posts on here and tried various different ways.

Here is my code at the moment:

    <script>
       $(document).ready(function () {
       // Handler for .ready() called.
       $('#MoveToCompanyId').attr("disabled", true);

       $('#DeleteAll').live("click", function () {

           if ($(this).attr("value") == "True") {
                 $('#MoveToCompanyId').attr("disabled", true);
           } else {
                 $('#MoveToCompanyId').attr("disabled", false);
                 $('#confirm').attr("disabled", true);
                 $('#MoveToCompanyId').change(function () {
                     if ($("#MoveToCompanyId option:selected").text() != "---Select Company---") {
                    $('#confirm').removeAttr("disabled");
                    alert($("#MoveToCompanyId option:selected").text());
                }
                else {
                    $('#confirm').attr("disabled", true);
                    alert("I should be disabled!");
                }

            });
        }
        });
     });

    </script>

Can anyone see any problems with it?

Just to clarify, i know it gets into the correct code blocks as my alerts are working. Its just the button disabling that is not working.

Kind Regards,

Gareth

Upvotes: 5

Views: 12453

Answers (3)

wirey00
wirey00

Reputation: 33661

You should be using

prop('disabled',true)
prop('disabled',false)

if you are using jQuery 1.6+ then you should be using prop.

Read more about prop

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. The .val() method should be used for getting and setting value.

Upvotes: 10

TRR
TRR

Reputation: 1643

Try

 attr('disabled', 'disabled')  

instead of

attr("disabled", true);

Upvotes: 0

Dave
Dave

Reputation: 3621

You should be using

.attr('disabled', 'disabled')

to disable a control and

.removeAttr('disabled')

to enable it.

http://www.w3schools.com/tags/att_input_disabled.asp

Upvotes: 3

Related Questions