Skip to content

Instantly share code, notes, and snippets.

@mazfreelance
Last active August 24, 2017 09:59
Show Gist options
  • Save mazfreelance/4d6980cff67df12a29b8c296db510c4b to your computer and use it in GitHub Desktop.
Save mazfreelance/4d6980cff67df12a29b8c296db510c4b to your computer and use it in GitHub Desktop.
JQUERY: Check/Uncheck with single button
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<SCRIPT language="javascript">
$(document).ready(function() {
$("#parent").click(function() {
var d = $(this).data();
$(".child").prop("checked", !d.checked);
// $('#parentnone').prop('checked', false);
//d.checked = !d.checked;
});
$("#parentnone").click(function() {
var d = $(this).data();
$('#parent').prop('checked', d.checked);
$(".child").prop("checked", false);
});
$('.child').click(function() {
if ($('.child:checked').length == $('.child').length) {
$('#parent').prop('checked', true);
} else {
$('#parent').prop('checked', false);
}
});
});
</SCRIPT>
<button id="parent" data-checked='false' style="background-color: transparent;border: none;">All</button> <br/>
<button id="parentnone" data-checked='false' style="background-color: transparent;border: none;">None</button> <br/>
<hr/>
<ul>
<li>
<input type="checkbox" class="child" /> Checkbox 1</li>
<li>
<input type="checkbox" class="child" /> Checkbox 2</li>
<li>
<input type="checkbox" class="child" /> Checkbox 3</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment