I’ve created a bootstrap switch and I have a div by the bottom of the switch so when the switch is on I want the div to show and when it’s off I want the div to hide. I find some solution which only shows for the checkbox.
How can I do it?
For example, if the switch checkbox is checked, then I need to show the content of div else hide the div.
.tags-list-group {
margin-bottom: 50px;
}
.tags-list-group .switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
float: right;
}
.tags-list-group .switch input {
display: none;
}
.tags-list-group .slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.tags-list-group .slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
.tags-list-group input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
.tags-list-group .slider.round {
border-radius: 34px;
}
.tags-list-group .slider.round:before {
border-radius: 50%;
}
.tags-list-group input.success:checked + .slider {
background-color: #8bc34a;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.4/js/bootstrap.min.js"></script>
<div class="tags-list-group">
<div class="tags-group-card">
<!-- Default panel contents -->
<ul class="list-group list-group-flush">
<li class="list-group-item custom-list-group-item">
User Defined
<label class="switch ">
<input type="checkbox" class="success">
<span class="slider round"></span>
</label>
</li>
</ul>
</div>
<div class="list-group-item show-content">
<p>Content 1(hide div)</p>
</div>
</div>
5
Answers
This is quite simple with jquery. Checkout the following example
you can just attach .click() on the slider and then use .toggle() to show/hide element, here is a working snippet:
Using JQuery you can achieve something like this:
It’s using the function, .show(), .hide() and the change event.
Use
prop
to get thethis
checked:This is what you wanted, based on your code. But, in my opinion, you can have a neater one by using collapse component of Bootstrap and embedding your switch inside it.