Check Checkbox Disabled (Jquery)
Asked Answered
L

3

14

The purpose is to;
If checkbox is disabled, do nothing.
If checkbox is enabled and checked, set the style of a button.
Here is what I've got so far;

 $(document).ready(function (e) {


        $(".checkbox").live("click", function () {

            if ($(this).hasAttribute('disabled')) {
                return false;
            }
            var isAnyChecked;

            $("input[type=checkbox]").each(function () {
                var checkedValue = $(this).attr("checked");
                if (checkedValue == "checked") {
                    isAnyChecked = true;
                }


            });

            if (isAnyChecked) {
                $("#<%= btnConfirm.ClientID %>").css("display", "block");

            } else {
                $("#<%= btnConfirm.ClientID %>").css("display", "none");

            }


        });  });

I've tried .is(':disabled'), .hasAttr(), .prop() and .attr(). Any help would be greatly appreciated.

Legist answered 10/10, 2012 at 14:20 Comment(1)
For starters, .live is deprecated and should be switched out for .onSnob
R
32

You have to check whether the disabled attribute is true: .attr('disabled').

Or, better, use .is(':disabled').---


EDIT: So it seems that .attr is now deprecated for this use (see http://api.jquery.com/attr/) The preferred way is:

$('#myCheckbox').prop('disabled')

It has to be noted that this still work as of today:

$('#myCheckbox').is(':disabled')

Try it here: https://jsfiddle.net/Robloche/phaqfrmj/

Rapallo answered 10/10, 2012 at 14:25 Comment(4)
No, you have to check for the property: .prop('disabled')Minorite
I've also tried on("change", function and not checking disabled completely. That did not work aswell.Legist
.prop works BUT not when page is loaded. Here is the thing, the grid in the page loads with selectedindex set to 0. When i select something else in the combobox, new items are listed and then the button is shown. But for the first time in page_load it does not work. I've checked the code behind nothing is wrong there tho.Legist
I made a mistake with .attr but it's fixed. Moreover, I tested the 3 solutions (.attr, .is and .prop) and they all work.Rapallo
T
2

Also you can try use this:

$("#myCheckBox").is('[disabled]');

Works for me to determine if an element is disabled.

Possible others solutions (.disabled, .is(':disabled'), .attr('disabled'), .prop('disabled')).

Terat answered 20/10, 2017 at 20:46 Comment(0)
B
0

for me, this works like a charm to disable:

$("#myCheckBox").prop("disabled", true) 

to enable:

$("#myCheckBox").prop("disabled", false)
Ballplayer answered 6/1, 2022 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.