Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unhighlight button after other button is clicked (Web CSS Bootstrap)

I have a signup page with two buttons. When one button is clicked, the corresponding container displays. When a button is clicked the bg color changes and sticks, even after I select the other button. Then, if I select the button again it goes back to its unselected/inactive color.

I want the clicked button to maintain its active color, but only if the other button is not clicked. If the other button is clicked, I want the first button to go back to its original bg color.

Here is the js:

<script type = "text/javascript">
  function displayForm(c) {
    if (c.value == "1") {

      document.getElementById("container1").style.display = 'inline';
      document.getElementById("container2").style.display = 'none';
    } else if (c.value == "2") {
      document.getElementById("container1").style.display = 'none';

      document.getElementById("container2").style.display = 'inline';
    } else {}
  } 
</script>

And here are the buttons (sorry for formatting issues):

                        <!--SELECTION BUTTONS-->
                            <form>
                                <div class="control-group">

                                    <label class="control-label">Are you a:</label>

                                    <div class="controls">

                                            <p><div id="account-type" name="account-type" class="btn-group selection-buttons" data-toggle="buttons-radio">

                                            <button value="1" type="button" name="formselector" onClick="displayForm(this)" id="button1" class="btn btn-info">
                                            Buttons1</button>

                                            <button value="2" type="button" name="formselector" onClick="displayForm(this)" id="button2" class="btn btn-info">Button2</button>

                                          </div></p>

                                    </div>

                                </div>
                            </form>

Here is the CSS (using Bootstrap):

/* SWITCH BUTTONS */
.selection-buttons button{
    width: 140px;
    height: 60px;
    color: #FFF;
    background-color: #FFB10D;
    border-color: #fff; /* e59f0b */
}

.selection-buttons .btn-info:hover, .btn-info:focus, .btn-info:active, .btn-info.active, .open .dropdown-toggle.btn-info {
    color: #FFF;
    background-color: #00CC66;
    border-color: #fff; /* 00b75b */
}

Thank you!!

like image 277
nhirschl Avatar asked Dec 05 '25 10:12

nhirschl


1 Answers

A pretty simple potential solution. In your JS just add the following lines:

 function displayForm(c) {
          for (var i = 1; i <= number_of_buttons; i++) {
            if (document.getElementById("button"+i) {
             document.getElementById("button"+i).className = "active";
          } else {
              document.getElementById("button"+i).className = "inactive";
       }
     }
   }

Then just use your CSS file to set the formatting you want for the active and inactive classes. If you don't have 1000+ buttons, this will be efficient enough for your needs.

like image 135
sulimmesh Avatar answered Dec 07 '25 11:12

sulimmesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!