Search code examples
javascripthtmlcssbootstrap-4bootstrap-modal

How to Create radio button group to the form to allow the selection of the smoking/non-smoking section of the restaurant?


I wish to emulate the behaviour of radio buttons withing a bootstrap form-group so a line in a form has several buttons ("btn btn-success" for example), with the buttons being able to be "selected", but only one of them can be selected at a given moment. I want a Output like this:

But I am getting this:

enter image description here

Also I am not able to select the Smoking button

The Code for the Following is :

 <div class="form-group row">
    <label for="section" class="col-12 col-md-2">Section</label>
       <div class="col-md-10">
         <div class="btn-group" data-toggle="buttons">
            <label class="btn btn-success active">
              <input type="radio" name="options" id="option1" autocomplete="off" checked>Non- 
               Smoking
            </label>
          <label class="btn btn-danger">
             <input type="radio" name="options" id="option2" autocomplete="off">Smoking
          </label>
        </div>
     </div>
 </div>

Solution

  • Adding btn-group-toggleto your btn-group div will solve the issue.

    <html>
    
    <head>
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
    </head>
    
    <body>
      <div class="form-group row">
        <label for="section" class="col-12 col-md-2">Section</label>
        <div class="col-md-10">
          <div class="btn-group btn-group-toggle" data-toggle="buttons">
            <label class="btn btn-success active">
                  <input type="radio" name="options" id="option1" autocomplete="off" checked>Non- 
                   Smoking
                </label>
            <label class="btn btn-danger">
                 <input type="radio" name="options" id="option2" autocomplete="off">Smoking
              </label>
          </div>
        </div>
      </div>
    </body>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
    
    </html>