$(".Button").click(function (e) {
e.preventDefault();
if (!$(this).hasClass("buttonActive")) {
$(this).addClass("buttonActive");
} else {
$(this).removeClass("buttonActive");
}
});
.buttonActive {
background: #100f10;
color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="numberSelect">
<button type="button" value="1" class="Button">1</button>
<button type="button" value="2" class="Button">2</button>
<button type="button" value="3" class="Button">3</button>
<button type="button" value="4" class="Button">4</button>
<input type="hidden" name="number" id="number">
</div>
What I wanna do is when I click on any button it checks if there is any other button has the class "buttonActive" and if any other button has this class it removes the class form the other button and add it to the button I clicked. So it should always be a single button which has the "buttonActive" class.