I have an online menu where I’d like to select either noodles or rice, but not both.
When I select a button some CSS happens.
I tried using jquery to do this and it still allows me to select both and doesn’t deselect the other.
Am I missing something glaringly obvious?
function selected(clicked) {
var ans=3 - clicked;
var noodles=$("1");
var rice=$("2");
var noodlesID=1;
var riceID=2;
switch (ans) {
case 1: document.getElementById(riceID).classList.toggle("select");
if (noodles.hasClass("select")) {
document.getElementById(noodlesID).classList.toggle("select");
}
break;
case 2: document.getElementById(noodlesID).classList.toggle("select");
if (rice.hasClass("select")) {
document.getElementById(riceID).classList.toggle("select");
}
break;
}
}
/* added by editor for demo purpose */
.selected {
border: 2px dashed red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="productimg" id="1" onclick="selected(this.id)">
<img src="images/plainnoodles.jpg" alt="">
<div class="productName">
Noodles
</div>
</button>
<button class="productimg" id="2" onclick="selected(this.id)">
<img src="images/plainrice.jpg" alt="">
<div class="productName">
Rice
</div>
</button>
2
Answers
There’s quite a few issues in your logic which need to be addressed:
#
.
select
yet in the CSS you define it asselected
onclick
. This is bad practice. Use delegated event handlers, bound in JS code. You can then use thethis
keyword within the event handlers to reference the element which raised the event.switch
or multipleif
conditions to change logic flow for multiple cases – otherwise you will have to update the JS every time the HTML changes, which is exactly what you need to avoid.remove()
the class from the buttons which were not clicked, nottoggle()
it.With all that said, here’s a working example which caters for an infinite number of buttons:
Note that the above is using plain JS. If you wanted to use jQuery instead, the code would look like this:
Just use the proper HTML element for this: two radio buttons.