skip to Main Content

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


  1. This is quite simple with jquery. Checkout the following example

    $(document).ready(() => {
        $("#content").hide();
    
        $("#chkbox").click((e) => {
            if ($("#chkbox").is(":checked")) {
                $("#content").show();
            } else {
                $("#content").hide();
            }
        });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <input type="checkbox" name="" id="chkbox" />
    <div id="content">
        <p>Hidden Content</p>
    </div>
    Login or Signup to reply.
  2. you can just attach .click() on the slider and then use .toggle() to show/hide element, here is a working snippet:

    $(document).ready(function(){
      $('.show-content').hide();
      $('.slider').click(function(e) {
        $('.show-content').toggle();
      })
    })
    .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>
    Login or Signup to reply.
  3. Using JQuery you can achieve something like this:

    $('.show-content').hide() // Init with hidden .show-content
    
    $('input.success').change(function() {
      if (this.checked) {
        $('.show-content').show()
      } else {
        $('.show-content').hide()
      }
    });
    .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.bundle.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>

    It’s using the function, .show(), .hide() and the change event.

    Login or Signup to reply.
  4. Use prop to get the this checked:

    $(function() {
      $('.success').change(function() {
        if ($(this).prop('checked')) {
          $(".show-content").show();
        } else {
          $(".show-content").hide();
        }
      })
    });
    .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;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <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>
    <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" style="display:none;">
        <p>Content 1(hide div)</p>
      </div>
    
    </div>
    Login or Signup to reply.
  5. 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.

    $("#myCustomCheckbox").change(function(){
    
      // in case the switch is on
      if(this.checked) {
            $("#myDivOfContent").addClass("content-area--show");
            
      // in case switch is off
        }else{
            $("#myDivOfContent").removeClass("content-area--show");
        }
    });
    /*** begin::div of content styles ****/
    
    .content-area {
      height: 0;
      width: 100%;
      overflow: hidden;
      transition: height .3s ease-in-out;
     }
     
     .content-area p {
      padding: 20px;
    }
     
    .content-area--show {
      height: 70px;
      border: 1px solid #ccc;
      border-top: 0;
    }
    
    .switch {
     margin-bottom: 0;
     }
    /*** end::div of content styles ****/
    
    .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" id="myCustomCheckbox">
                        <span class="slider round"></span>
                    </label>
                </li>
            </ul>
        </div>
        <div class="content-area" id="myDivOfContent">
            <p>Content 1(hide div)</p>
        </div>
    
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search