I was working with some javascript code and i have a list of buttons with the same class names but i wanted to know which button among them is clicked inorder to get the value. How can I do that with vanilla js? Here is the sample code
<button onclick="chooseRating()" id="rateBtn" class="rateBtn" value="1">1</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="2">2</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="3">3</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="4">4</button>
<button onclick="chooseRating()" id="rateBtn" class="rateBtn"value="5">5</button>
and the js code i tried is
const rateBtn = document.getElementsByClassName("rateBtn");
function chooseRating() {
for (rate in rateBtn) {
console.log(rateBtn[rate]);
if (rateBtn[rate].clicked === true) {
rating = rateBtn[rate].value;
console.log(rating);
//shows 5 don't know why
}
}
}