Skip to content

Instantly share code, notes, and snippets.

@momin-riyadh
Created May 26, 2024 10:58
Show Gist options
  • Save momin-riyadh/bbdef9e20674b95da25e9d46ca618711 to your computer and use it in GitHub Desktop.
Save momin-riyadh/bbdef9e20674b95da25e9d46ca618711 to your computer and use it in GitHub Desktop.
On active switch toggle collapse show and hide
<!-- https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/js/bootstrap.min.js -->
<!-- https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css -->
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="user_active" name="user_active">
<label class="form-check-label" for="user_active">Select User Status</label>
</div>
<div class="collapse" id="myCollapse">
<div class="card card-body">
This div will be shown or hidden based on the toggle switch.
</div>
</div>
const toggleSwitch = document.getElementById('user_active');
const collapseInstance = new bootstrap.Collapse('#myCollapse', {
toggle: false
});
toggleSwitch.addEventListener('change', function() {
if (this.checked) {
collapseInstance.show();
} else {
collapseInstance.hide();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment