Find the closest ancestor element that has a specific class.
npm install -S @jswork/next-dom-find-ancestor
<div class="far ancestor">
<div id="target2">
<div class="near ancestor">
<p id="target1">Where am I?</p>
</div>
</div>
</div>
import '@jswork/next-dom-find-ancestor';
const el1 = document.querySelector('#target1');
const el2 = document.querySelector('#target2');
nx.domFindAncestor(el1, '.ancestor'); // near
nx.domFindAncestor(el2, '.ancestor'); // far
Code released under the MIT license.