function toggleButton1() {
$("#button1").toggleClass("button-active button-inactive");
$("#button2").toggleClass("button-inactive button-active");
$("#button3").toggleClass("button-inactive button-active");
}
function toggleButton2() {
$("#button2").toggleClass("button-active button-inactive");
$("#button1").toggleClass("button-inactive button-active");
$("#button3").toggleClass("button-inactive button-active");
}
function toggleButton3() {
$("#button3").toggleClass("button-active button-inactive");
$("#button2").toggleClass("button-inactive button-active");
$("#button1").toggleClass("button-inactive button-active");
}
.button-active {
display: inline-block;
border: 1px solid #CCC;
background-color: white;
box-shadow: 0px 0px 4px rgb(130, 130, 130) !important;
cursor: pointer;
vertical-align: middle;
max-width: 100%;
padding: 16px;
text-align: center;
border-radius: 4px;
color: rgb(130, 130, 130) !important; }
.button-inactive {
display: inline-block;
border: 1px solid #CCC;
background-color: #e7f6fe;
box-shadow: 0px 0px 4px rgb(130, 130, 130) !important;
cursor: pointer;
vertical-align: middle;
max-width: 100%;
padding: 16px;
text-align: center;
border-radius: 4px;
color: rgb(130, 130, 130) !important; }
.button-hover {
color: rgb(130, 130, 130) !important;
background-color: #e7f6fe;
box-shadow: 0px 0px 4px rgb(130, 130, 130) !important;
border-radius: 8px;
margin-right: 0px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-12">
<div class="row">
<div class="col-md-4 button-active" id="button1" onclick="toggleButton1()">
<div class="col-12">Update 1</div>
</div>
<div class="col-md-4 button-active" id="button2" onclick="toggleButton2()">
<div class="col-12">Update 2 </div>
</div>
<div class="col-md-4 button-active" id="button3" onclick="toggleButton3()">
<div class="col-12">Update 3 </div>
</div>
</div>
</div>
Hello, I have created these div buttons that should act as individual selectable options with different colors for each when active.
Clicking one button will activate it and deactivate the others.
When a button is activated, its background color should change to blue.
The hover effect color should be consistent with the selected color.
I have used JavaScript toggling functions for this functionality, but encountered an issue where only one button can be toggled at a time:
(function toggleButton1() { $("#button1").toggleClass("button-active button-inactive");)}
The above JavaScript function works correctly for a single button, but applying it to other buttons selects all of them on click. Please assist me with this. Thank you.