I need to check a radio button when the user clicks anywhere in the parent li. The ul is in the original html but the li and radio button are rendered to the DOM in a for loop inside a function below (displaySearchResults(responseJson).
Have not been able to apply any change to the radio button for the specific clicked li (sometimes able to change all radio buttons) -- no luck using the this keyword or various class/id/element combinations to target children of the clicked li.
Here is the event listener followed by relevant code.
This has been an absolute nightmare, any help is appreciated. Thanks!
EVENT LISTENER
function select(){
$('.results-list-item').click(event => {
let name = $(this)
console.log('li click')
//various attempts at marking the child radio button as checked
//$(this).closest($('input')).prop("checked", true)
//$(this).children($('input')).attr('checked','true')
$('.results-list-item').children($('input.radio')).attr('checked','')
})
};
DISPLAY
function displaySearchResults(responseJson) {
for (let i = 0; i < 10; i++){
$('#results-list').append(
`
<li class='results-list-item'>
<h3>${results[i].name}</h3>
<input type='radio' id='${results[i].name}' value='${responseJson.results[i].id}' required>
</li>
`
)
}
select()
}
HTML
<main class='container'>
<section id='results'>
<form id='js-results-form'>
<ul id='results-list'>
</ul>
<input type='submit' value='Select Game'></input>
</form>
</section>
</main>