Php/WebStorm is reporting this wierd warning in my JavaScript code stating that property id
is not defined in the element/node. Thing is, the element is a firstChild
element fetched from the parent element.
function hideChildAndUpdateID(title) {
let parent = document.getElementById('parent');
let child = parent.firstChild;
// Hiding the child.
child.style.display = 'none';
// Temporary ID so we can find it later.
child.id = 'child-tmp-id'; // Warning occurs here
}
And this is the entire message from Php/WebStorm
Property id is not defined in type Node less... (Ctrl+F1)
Inspection info: This inspection reports assignments to undefined properties of explicitly type-annotated variables.
This code itself actually works. The child
element gets it's id
changed to whatever I set it to, but this warning is anoying me.
I have tried using child.setAttribute('id', 'child-tmp-id')
, but that did not work properly.
Is there a convention when dealing with firstChild
or it's just a Php/WebStorm thingy?
Nodes can be more than just elements and don't necessarily have an id
property. Try using firstElementChild
instead that returns the first child that is an element:
function hideChildAndUpdateID(title) {
let parent = document.getElementById('parent');
let child = parent.firstElementChild;
// Hiding the child.
child.style.display = 'none';
// Temporary ID so we can find it later.
child.id = 'child-tmp-id';
}