Skip to content

Instantly share code, notes, and snippets.

@johnmeehan
Created October 18, 2020 18:34
Show Gist options
  • Save johnmeehan/6f18373f39b07b2173244bf606862563 to your computer and use it in GitHub Desktop.
Save johnmeehan/6f18373f39b07b2173244bf606862563 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";
}
}
}
@Viktor1953
Copy link

Great job - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment