Search code examples
togglebootstrap-4

Radio button toggle expand using Bootstrap 4


I am trying to use Bootstrap 4 to create a form that expands different elements as each "button" is clicked.

How it should work: Buttons(radio): YES / NO --- If "YES" is clicked then it expands an element containing checkbox buttons.

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary active" data-toggle="collapse" href="#collapseExample" aria-expanded="false"> 
    <input type="radio" name="options" id="option2" autocomplete="off"> Yes
  </label>
  <label class="btn btn-primary">
    <input type="radio" name="options" id="option3" autocomplete="off"> No
  </label>
</div>
<!-- Expanded Buttons -->
<div class="btn-group collapse" id="collapseExample" data-toggle="buttons">
  <label class="btn btn-primary active">
    <input type="checkbox" autocomplete="off"> Checkbox 2
  </label>
  <label class="btn btn-primary">
    <input type="checkbox" autocomplete="off"> Checkbox 3
  </label>
</div>
<!-- / Expand Buttons -->

Here's a link to my Pen.


Solution

  • If you wanted to do it using only native Bootstrap 4 classes (without any custom css), then this would be one way of doing it:

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <div>
        <button class="navbar-toggler pl-1" type="button" data-toggle="collapse" data-target="#collapseExample" aria-controls="collapseExample" aria-expanded="false" aria-label="Toggle navigation">
            <div class="form-check">
                <label class="btn btn-primary active" data-toggle="collapse" data-target="#collapseExample" href="#collapseExample" aria-expanded="false"> 
                    <input type="radio" name="options" id="option2" autocomplete="off"> Yes
                </label>
            </div>
        </button>
        <label class="btn btn-primary">
            <input type="radio" name="options" id="option3" autocomplete="off"> No
        </label>
    
        <div class="collapse pl-4" id="collapseExample">
            <!-- Expanded Buttons -->
            <div class="btn-group collapse" id="collapseExample">
                <label class="btn btn-primary active">
                    <input type="checkbox" autocomplete="off"> Checkbox 2
                </label>
                <label class="btn btn-primary">
                    <input type="checkbox" autocomplete="off"> Checkbox 3
                </label>
            </div>
            <!-- / Expand Buttons -->
        </div>
    </div>
    
    
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>