Created
November 4, 2015 17:57
-
-
Save iamrobert/96336a51ee8d0d0c35ac to your computer and use it in GitHub Desktop.
CHECKBOX SELECT ALL ON LOAD with SELECT/DESELECT BUTTON
This file contains hidden or 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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>SELECT ALL</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> | |
</head> | |
<body> | |
<fieldset> | |
<input type="checkbox" class="select_all" /> Select all<br> | |
<input type="checkbox" class="checkbox" value="1"/>Item 1<br> | |
<input type="checkbox" class="checkbox" value="2"/>Item 2<br> | |
<input type="checkbox" class="checkbox" value="3"/>Item 3<br> | |
<input type="checkbox" class="checkbox" value="4"/>Item 4<br> | |
<input type="checkbox" class="checkbox" value="5"/>Item 5<br> | |
</fieldset> | |
<fieldset> | |
<input type="checkbox" class="select_all" /> Select all<br> | |
<input type="checkbox" class="checkbox" value="1"/>Item 1<br> | |
<input type="checkbox" class="checkbox" value="2"/>Item 2<br> | |
<input type="checkbox" class="checkbox" value="3"/>Item 3<br> | |
<input type="checkbox" class="checkbox" value="4"/>Item 4<br> | |
<input type="checkbox" class="checkbox" value="5"/>Item 5<br> | |
</fieldset> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
//SELECT ALL | |
$('input:checkbox').attr('checked', 'checked'); | |
$('.select_all').on('click',function(){ | |
if(this.checked){ | |
$(this).parents('fieldset:eq(0)').find('.checkbox').each(function(){ | |
this.checked = true; | |
}); | |
}else{ | |
$(this).parents('fieldset:eq(0)').find('.checkbox').each(function(){ | |
this.checked = false; | |
}); | |
} | |
}); | |
$('.checkbox').on('click',function(){ | |
if($('.checkbox:checked').length == $('.checkbox').length){ | |
$(this).parents('fieldset:eq(0)').find('.select_all').prop('checked',true); | |
}else{ | |
$(this).parents('fieldset:eq(0)').find('.select_all').prop('checked',false); | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment