Reputation: 165
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>
Upvotes: 2
Views: 4240
Reputation: 87073
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)
.
Upvotes: 4