Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Alpine JS - Checkboxes With All/None Toggle Links
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<script src="" defer></script>
We have to use $refs because x-bind:checked updates the HTML not the prop.
toggle(checked) {
.forEach(c => c.checked = checked);
<a href="#" @click.prevent="toggle(true)">All</a> | <a href="#" @click.prevent="toggle(false)">None</a>
<ul x-ref="checkboxes">
<li><label><input type="checkbox" value="a">A </label></li>
<li><label><input type="checkbox" value="b">B </label></li>
<li><label><input type="checkbox" value="c">C </label></li>
<li><label><input type="checkbox" value="d">D </label></li>
<li><label><input type="checkbox" value="e">E </label></li>
<li><label><input type="checkbox" value="f">F </label></li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment