Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 265
Caitlyn Avatar asked Jan 21 '18 16:01

Caitlyn


People also ask

How do I toggle a button in Bootstrap?

Add data-toggle="button" to toggle a button's active state. If you're pre-toggling a button, you must manually add the . active class and aria-pressed="true" to the <button> .

What is the use of collapse in Bootstrap?

The collapse JavaScript plugin is used to show and hide content. Buttons or anchors are used as triggers that are mapped to specific elements you toggle. Collapsing an element will animate the height from its current value to 0 .


1 Answers

Here is a simple solution, that shows the div when Yes is checked, and hides it if No is checked:

<label>
    <input type="radio" name="radio" data-toggle="collapse" data-target="#collapseExample:not(.show)"> Yes
</label>
<label>
    <input type="radio" name="radio" data-toggle="collapse" data-target="#collapseExample.show"> No
</label>

<div class="collapse" id="collapseExample">
    Gets toggled by radios
</div>

FIDDLE

like image 118
codeflorist Avatar answered Sep 28 '22 11:09

codeflorist