Unresolved variable checked

Hi,

 

I am using this code:

document.getElementById('member').addEventListener('change', (event) => {
if (event.target.checked) {
console.log('Hello');
}
});

but Intellij gives me the warning:

Unresolved variable checked

Why do I get this warning? My code works fine and I get the console log.

0
3 comments

The IDE has no idea what your target element is, and EventTarget interface doesn't have checked property. You need to explicitly tell the IDE the type of the HTMLElement which is your target. For example, in Typescript this would be:

document.getElementById('member').addEventListener('change', (event) => {
const checkbox: HTMLInputElement = <HTMLInputElement>event.target;
if (checkbox.checked) {
console.log('Hello');
}
});
0

And how would you do it with JavaScript?

0
document.getElementById('member').addEventListener('change', (event) => {
const checkbox = /**HTMLInputElement*/event.target;
if (checkbox.checked) {
console.log('Hello');
}
});
0

Please sign in to leave a comment.