I am trying to learn pure javascript. I know jquery to do stuff quickly. Now i am trying to get children of siblings in pure js and even i got all siblings of currentTarget class but now i want child of siblings like we do in jquery $(this).parent().siblings().children();
this is my javascript
var rootParent = e.currentTarget.parentNode.parentNode.parentNode;
var childClass = this.siblings(rootParent);
console.log(childClass.childNodes);
but when i am trying to console its gives me undefined.
If the console shows both “e” and “this” to be what you expect them to be, but “childClass.childNodes” is undefined, then something else is not what you are expecting it to be. Try
var rootParent = e.currentTarget.parentNode.parentNode.parentNode;
console.log(rootParent);
var childClass = this.siblings(rootParent);
console.log(childClass.childNodes);
if “rootParent” looks OK (eg. there are that many parent nodes and it returns it) then that would narrow it down to “siblings” not returning what you want it to.
If “rootParent” looks OK please post the code for the siblings function.
As @Mittineague said, that would probably be the crucial bit where the problem occurs. As the name suggests, I assume that this function returns an array or node list containing the siblings; such lists don’t have a .childNodes property, so you’d have to iterate over the elements manually first. For example like
const siblings = node => Array
.from(node.parentNode.children)
.filter(sibling => sibling !== node)
const cousins = node => siblings(node.parentNode)
.reduce((children, element) => {
// Get the children of the current element
const currentChildren = Array.from(element.children)
// ...and append them to the child list
return children.concat(currentChildren)
}, [])