Skip to content

Instantly share code, notes, and snippets.

@sergii
Forked from johnmeehan/index.html
Created May 11, 2024 23:06
Show Gist options
  • Save sergii/23d3fcb2ec8daecdfeb3977d28f64501 to your computer and use it in GitHub Desktop.
Save sergii/23d3fcb2ec8daecdfeb3977d28f64501 to your computer and use it in GitHub Desktop.
Stimulus js Checkbox toggle
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="main.css">
<script src="bundle.js" async></script>
</head>
<body>
<div data-controller="toggler">
<label>Toggle Me</label>
<input data-action="input->toggler#toggle" data-target="toggler.input" type="checkbox" id="someid" name="someid" checked>
<div data-target="toggler.hideme" id="stuff_to_hide">
<h1>
Importand stuff to hide
</h1>
</div>
</div>
</body>
</html>
#stuff_to_hide {
display: none;
}
import { Controller } from "stimulus"
export default class extends Controller {
static targets = [ "input", "hideme" ];
connect() {
this.toggle();
}
toggle() {
if (this.inputTarget.checked) {
this.hidemeTarget.style.display = "block";
} else {
this.hidemeTarget.style.display = "none";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment