How can I dynamically change the opacity of an image using JavaScript when a radio button is clicked?
I attempted to achieve this using a forEach function, but the current code causes all images to instantly switch to opacity 1;
Additionally, after refreshing the page, the radio button remains selected.
let img = document.querySelectorAll('.poll_img');
let radio = document.querySelectorAll('.radio_button');
radio.forEach(radioButton => {
if (radioButton.checked === true){
img.forEach(image => {
image.style.opacity = 1;
})
}
})
<form class="poll_form" action="">
<div class="poll_bar">
<img class="poll_img poll_img1" src="assets/images/poll_bar-icon.svg" alt="">
<label class="radio_title" for="radio1">Atlantic Forest, South America
</label><input class="radio_button" type="radio" name="group1" id="radio1">
<div class="check"></div>
</div>
<div class="poll_bar">
<img class="poll_img poll_img2" src="assets/images/poll_bar-icon.svg" alt="">
<label class="radio_title" for="radio2">Borneo Island, Southeast Asia
</label><input class="radio_button" type="radio" name="group1" id="radio2">
<div class="check"></div>
</div>
</form>