Created
October 18, 2020 18:34
-
-
Save johnmeehan/6f18373f39b07b2173244bf606862563 to your computer and use it in GitHub Desktop.
Stimulus js Checkbox toggle
This file contains 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"> | |
<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> |
This file contains 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
#stuff_to_hide { | |
display: none; | |
} |
This file contains 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
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
Thanks for the example, helped me a lot :)