2

I'm trying to disable a checkbox in the checkbox list using jQuery. But it doesn't seem to work. Here's the code. The checkbox list has a total of 12 checkboxes.

<script src="../AutoComplete Jquery/jquery-1.7.2.min.js" type="text/javascript">  
</script>
<script type="text/javascript">
$(document).ready(function() 
{
var disable = 5;
var i=0;
$(":checkbox").each(function()
                {
                    if(i<disable)
                    {
                        $(this).attr("disabled", "disabled");
                        i=i+1;
                    }
                });
});
</script>

1 Answer 1

4

You can try this:

$(':checkbox:lt(5)').attr('disabled', 'disabled');

OR

$(':checkbox:lt(5)').prop('disabled', true);

According to your approach:

$(":checkbox").each(function(i, check) {
    if (i < disable) {
        $(this).attr("disabled", "disabled");
    }
});

Within the .each() callback function first parameter is the index of checkbox. So you don't need to keep i for indexing.

To enable the checkbox again:

.removeAttr('disabled'); or .prop('disabled', false).

Sign up to request clarification or add additional context in comments.

3 Comments

@Prince check the last answer.. may be it'll help you
thanks dude...it's working fine now. n how can i enable the checkbox
Best way to present gratitude is to accept the answer @Prince.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.