No JQuery please.
I'm quite surprised not to have found the answer to this. I'll probably be "duped".
The problem is to get only the immediate child elements with a given tag name (e.g. DIV), so getElementsByTagName
doesn't work: this will return all descendants with that tag name.
There's a nice-looking answer here, using querySelectorAll()
:
const childDivs = document.querySelectorAll('#xyz > div');
... but what I've done is just create an element (DIV) on the fly and made its innerHTML
equal to the content of a fetch
'd HTML file. The consequence of this is to make the BODY's child elements become the child elements of the created element.
So it doesn't have an ID or class, and it's not even in the DOM tree. Of course I can iterate ... but who wants to iterate in 2020! Preposterous. So how might I say:
const childDivs = container.querySelectorAll('ME > DIV');
...?
Edit
Had a go with the filter
idea from Barmer:
const childEls = container.children
function checkDiv( el ){
return el.tagName === 'DIV'
}
const childDivs = childEls.filter( checkDiv )
error: "childEls.filter is not a function"
... I think childEls
(class HTMLCollection) above is one of those "Array-like" objects. So I believe you have to do this:
const childEls = container.children
const newArr = Array.prototype.slice.call( childEls );
const childDivs = newArr.filter( function( el ){ return el.tagName === 'DIV' } )
... which works... but is, in truth, iteration. Boo.