0

hey guys so im trying to check if a checkbox is checked. and if one checkbox is checked a button is enabled if more than 1 it is still disabled; button is disabled by default.

<script type="text/javascript">
    var editbtn = $("#edit_contact");
    editbtn.attr("disabled","disabled");

    $('.contacts input:checkbox').live("click", function()
    {
        if(this.checked.length === 1)
        {
            editbtn.removeAttr("disabled");
        }
        else
        {
            editbtn.attr("disabled","disabled");
        }
    })
</script>

what am i doing wrong?
TIA

2
Contributors
2
Replies
30
Views
3 Years
Discussion Span
Last Post by nadiam
-1

jQuery's live function is deprecated. You should use jQuery's ON function, instead.

Also, since you didn't post your html along with this script, can you clarify if all the checkboxes you're working with have an ID of "contacts"? Since, I noticed you used:
$('.contacts input:checkbox')
which would select all checkboxes which are the children of an element with the class "contacts". This is ambiguous, therefore, please clarify what you intended to select. I'm assuming for the sake of this solution, that all your checkboxes have a common class of "contacts" and you were intending to select all of them. With this assumption in mind, the code below should work for you. Notice the changes I made with respect to the code you posted:

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
        $("#edit_contact").attr("disabled","disabled");

        $('.contacts:checkbox').on("click", function() {
            if(this.checked.length === 1)
            {
                $("#edit_contact").removeAttr("disabled");
            }
            else
            {
                $("#edit_contact").attr("disabled","disabled");
            }
        });
    </script>
</head>

<body>
</body>
</html>
0

it doesn't seem to be working though. button doesn't get enabed when checkbox clicked

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.