The issue might to be related to either the absence of a value attribute in the HTML for your radio buttons or an incorrect HTML structure that doesn’t allow the document.querySelector('input[type="radio"]:checked') to find the checked radio button correctly.
Some things to check:
Check i your HTML and verify that your radio buttons have a value attribute and are grouped under the same name attribute. Here’s an example:
Each radio button should have:
• A name attribute to group them (e.g., emotion).
• A unique id for individual identification.
• A value attribute that represents the radio button’s selection value.
Check highlightCheckedOption Behavior
Your highlightCheckedOption function appears to modify classes for radio buttons’ parent elements. Make sure to check document.getElementById(e.target.id) in this function correctly targets the clicked radio button’s parent element.
Test the getMatchingCatsArray Function with some console.logs
function getMatchingCatsArray() {
const checkedRadio = document.querySelector('input[type="radio"]:checked');
if (checkedRadio) {
const selectedEmotion = checkedRadio.value;
console.log(selectedEmotion); // Should log the value of the selected radio button
} else {
console.log("No radio button is checked.");
}
}
If the radio buttons are correctly set up, clicking the button should log the value of the selected radio button.