OiO.lk Blog javascript Javascript: get the closest Node, not the closest Element
javascript

Javascript: get the closest Node, not the closest Element


I want to query all the <div> from the div element of class tab-content, which is a sibling of the div element with class tabs, which is a parent of the current <li> element.

Suppose tab is one of the tabs and the current node. This code:

const tabContent = tab.closest('.container').querySelectorall('.tab-content > div');

is returning an error querySelectorall is not a function, because closest() is returning an Element, not a Node.

So how to get that freaking closest node instead of the element (in pure JavaScript, no JQuery) ?

Here’s the HTML:

<div class="container">
    <div class="tabs">
        <ul>
            <li>Tab 1</li>
            <li>Tab 2</li>
            <li>Tab 3</li>
            <li>Tab 4</li>
        </ul>
    </div>
    <div class="tab-content">
        <div id="id1">Content for Tab 1</div>
        <div id="id2">Content for Tab 2</div>
        <div id="id3">Content for Tab 3</div>
        <div id="id4">Content for Tab 4</div>
    </div>
</div>



You need to sign in to view this answers

Exit mobile version