in typescript I need to collect all div of a page and using below code
const getHTMLElement() : HTMLElement[] {
const doc = document.body.children;
const listL HTMLElement[] = [];
for (let c of Array.from(doc)) {
if (c.tagName === 'DIV' && c.textContent !== '') {
list.push(c);
}
}
console.log({list})
}
but it give ts error on .push(c)
line
Argument of type ‘Element’ is not assignable to parameter of type ‘HTMLElement’.
what is wrong here?
tried with querySelectorAll()
const getHTMLElement = (): HTMLElement[] {
const doc = document.querySelectorAll('body');
const list = [];
for (let c of Array.from(doc)) {
if (c.tagName === 'DIV' && c.textContent !== '') {
list.push(c);
}
}
console.log({list})
}
although this doesn’t give any type error but here type of c
is HTMLBodyElement
which seems me incorrect
so What is the write way to get list of HTMLElement[]
type