You should better use hasAttribute('checked')to check if it's checked and `setAttribute('checked', true)' to check it:
function check(){
if (!document.getElementById('myCheck').hasAttribute('checked'))
document.getElementById('myCheck').setAttribute('checked' ,true);
}
document.getElementById('check').addEventListener('click', check);
You have to cast/convert the result of
getElementById
a inHTMLInputElement
order to access the propertychecked
:You should better use
hasAttribute('checked')
to check if it's checked and `setAttribute('checked', true)' to check it:Reference:
checked - MDN