Last active
August 29, 2015 14:25
-
-
Save avtaniket/7a569e0642cbcbd6227a to your computer and use it in GitHub Desktop.
Checkbox Checked-all-none-toggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="" method="POST" name="tenants"> | |
<table class="list"> | |
<caption>Users</caption> | |
<thead> | |
<tr> | |
<th> </th> | |
<th>Name</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr class="highlight"> | |
<td><input type="checkbox" class="ckb" name="ids[]" value="1"></td> | |
<td><a href="#">Anyee</a></td> | |
</tr> | |
<tr class="highlight"> | |
<td><input type="checkbox" class="ckb" name="ids[]" value="2"></td> | |
<td><a href="#">John</a></td> | |
</tr> | |
</tbody> | |
<tfoot> | |
<tr> | |
<td colspan="2">Select: <a id="selectAll" href="#ckb">All</a> | |
<a id="selectNone" href="#ckb">None</a> <a | |
id="selectToggle" href="#ckb">Toggle</a> | |
</td> | |
</tr> | |
</tfoot> | |
</table> | |
</form> | |
<script type="text/javascript"> | |
$(function(){ | |
$('table.list input:checkbox:checked').trigger('change'); | |
$('#selectAll').click(function(e) { | |
e.preventDefault(); | |
var target = $(this).attr('href').substr(1, $(this).attr('href').length); | |
$(this).closest('form') | |
.find('input:enabled:checkbox.'+target) | |
.prop('checked', true) | |
.trigger('change'); | |
return false; | |
}); | |
$('#selectNone').click(function(e) { | |
e.preventDefault(); | |
var target = $(this).attr('href').substr(1, $(this).attr('href').length); | |
$(this).closest('form') | |
.find('input:enabled:checkbox.'+target) | |
.prop('checked', false) | |
.trigger('change'); | |
return false; | |
}); | |
$('#selectToggle').click(function(e) { | |
e.preventDefault(); | |
var target = $(this).attr('href').substr(1, $(this).attr('href').length); | |
$(this).closest('form') | |
.find('input:enabled:checkbox.'+target) | |
.each(function() { | |
$(this) | |
.prop('checked', !$(this).is(':checked')) | |
.trigger('change'); | |
}); | |
return false; | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment