3

Can someone tell me all possible ways to disable all buttons in a class, I already tried this which isn't working,

$("input.myClass").attr('disabled', true);

This is how I am using it,

    <script type="text/javascript">
        $(document).ready(function() {

            var objA = $get('<%= checkbox.ClientID %>');

            $(objA).change(function() {

                if($(this).is(':checked'))
                {  
                    $(".anotherClass").sortable('disable'); //works
                    $("input.myClass").prop('disabled', 'disabled'); //doesn't work
                }

CSS is quiet simple,

.myClass
{
    float: left; 
    width: 140px; 
    margin-left: 4px; 
    margin-right: 4px; 
    padding-top: 20px;
    text-align: center;
}

HTML looks like this,

    <div class="myClass">
        <asp:Button ID="btn1" Text="bbb" runat="server" CausesValidation="False" CssClass="button" OnClientClick="return false;" />
        <asp:Button ID="btn2" Text="aaa" runat="server" CausesValidation="False" CssClass="button" OnClientClick="return false;" />
        <asp:Button ID="btn3" runat="server" CausesValidation="False" CssClass="button" OnClick="bt_Click" />
    </div>
Jim G.
  • 14,056
  • 19
  • 94
  • 153
Mathematics
  • 6,404
  • 20
  • 65
  • 133

7 Answers7

3

jQuery 1.6+

To change the disabled property you should use the .prop() function.

$("input.myclass").prop('disabled', true);
$("input.myclass").prop('disabled', false);

jQuery 1.5 and below

The .prop() function doesn't exist, but .attr() does similar:

Set the disabled attribute.

$("input.myclass").attr('disabled','disabled');

To enable again

$("input.myclass").removeAttr('disabled');

Referred From : Stack Question

Community
  • 1
  • 1
Sanober Malik
  • 2,529
  • 19
  • 29
1

Use .prop

$("input.myClass").prop("disabled", true);

If you want to disable all button type inputs inside div with myClass class:

$(".myClass input[type=button]").prop("disabled", true);
Aleksandr M
  • 23,647
  • 12
  • 63
  • 129
1

If you want to use attr(), use this:

$("input.myClass").attr('disabled', 'disabled');

Or prop():

$("input.myClass").prop('disabled', true);

In regard to updated question:

  1. Change your script tag to <script>
  2. Place it towards the bottom of the page (not in the head section)
dsgriffin
  • 61,907
  • 17
  • 128
  • 134
1

To disable

$('input.myClass').attr('disabled', 'disabled');

// To enable

$('input.myClass').removeAttr('disabled');

// OR you can set attr to ""

$('input.myClass').attr('disabled', '');

Some SO Links
jQuery disable/enable submit button
JQuery - Set Attribute value
.attr("disabled", "disabled") issue
Toggle input disabled attribute using jQuery

Community
  • 1
  • 1
शेखर
  • 16,910
  • 12
  • 52
  • 105
1

since you asked all possible ways and the common ones are out there you can always prevent default by doing:

$('input.myClass').click(function(e){
    e.preventDefault();
})

preventing default, prevents from submitting a form for example.

Matthias Wegtun
  • 1,221
  • 9
  • 13
1

You can use $("input[type='button']").attr("disabled","disabled"); to disable all buttons.

web2008
  • 868
  • 4
  • 11
0
$(".myClass :input").attr('disabled', true);

is the only code that worked for me.

Mathematics
  • 6,404
  • 20
  • 65
  • 133