Okay so since my professor told me about this article Handling Events for Many Elements
I want to rewrite some of my old Javascript which apparently uses “bad practice”
HTML
<div id="voteParent">
<input id="vote1" type="radio" name="voting" value="1">
<label for="vote1">Vote 1</label>
<input id="vote2" type="radio" name="voting" value="2">
<label for="vote2">Vote 2</label>
<input id="vote3" type="radio" name="voting" value="3">
<label for="vote3">Vote 3</label>
<input id="vote4" type="radio" name="voting" value="4">
<label for="vote4">Vote 4</label>
<input id="vote5" type="radio" name="voting" value="5">
<label for="vote5">Vote 5</label>
</div>
Old javascript
function starRating() {
var vote = document.getElementsByClassName('vote');
var voteL = vote.length;
for (let i = 0; i < voteL; i++) {
let voteValue = vote[i].value;
vote[i].addEventListener('click', function () {
var imgValue = document.getElementById("imgValue").value;
newImage(voteValue, imgValue, checked, "rating");
console.log(vote[i].value);
if (document.getElementById("globalRating")) {
var globalRating = document.getElementById("globalRating");
if (hasClass(globalRating, "leftIn")) {
globalRating.classList.remove("leftIn");
globalRating.classList.add("leftOut");
}
}
});
}
}
The new Javascript i am working on
var voteParent = document.querySelector("#voteParent");
voteParent.addEventListener("click", starRating, false);
function starRating(e){
if(e.target !== e.currentTarget){
var clickedVote = e.target.value;
alert("Target er = " + clickedVote);
}
e.stopPropagation();
}
But my problem here is that the code tries to take the value from both the label and the input field, what do i do here so that when i click the label it does only store the value of the input?