My goal is to create a button click function that allows only one button to be clicked at a time. For instance, when btn1 is clicked, its background color changes from transparent to green. Then, if btn2 is clicked, btn1's background color should change from green to red, and btn2's background color should become transparent. Additionally, I want to implement a toggle feature for both buttons.
function myFunction(str) {
var element = document.getElementById("btn1");
var element1 = document.getElementById("btn2");
if(str==1){
document.getElementById("btn2").style.backgroundColor ="";
element.classList.toggle("mystyle");
}else{
element1.classList.toggle("mystyle1");
document.getElementById("btn1").style.backgroundColor ="";
}
}
.mystyle {
background-color: green;
color: white;
}
.mystyle1 {
background-color: red;
color: white;
}
<button class="" id="btn1" onclick="myFunction(1)">Upvote </button>
<button class=""id="btn2" onclick="myFunction(-1)">Down Vote</button>