$('.child_1').parents('.parent').find('.child_2')
How do I call the above JQuery syntax using core javascript syntax?
I am calling the child_2
using child_1
who has a common parent
$('.child_1').parents('.parent').find('.child_2')
How do I call the above JQuery syntax using core javascript syntax?
I am calling the child_2
using child_1
who has a common parent
$('.child_1').closest('.parent').find('.child_2');
closest()
will get you the first parent that matches the given query string.
You can do like this:
$('.child_1').parents('.parent:first').find('.class:eq(1)');
First notice that I have used :first
to determine the proper parent of the child_1.
Then,
I have put in eq(1)
because child counting starts from 0;
Refer to this answer to Get parents of element in JS and then iterate over the parents to get their child using querySelector
var parents = getParents( document.getElementByClassName('.child_1')[0]);
var children = [];
parents.forEach(function (parent) {
children.push(parent.querySelector('.child_2'));
});