Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a parent element (without using Jquery)

Tags:

javascript

dom

I just need to access the parent div where I have a button changing his siblings divs. A code example can explain better:

<div class="parent">  <!-- This is structure repeats N times -->
    <div class="divToToggleVisiblity divA">trololo A</div>
    <div class="divToToggleVisiblity divB">trololo B</div>
    <button onClick="toggleThem(this)">This button will toggle above divs</button>
</div>


function toggleThem(a){  // something like this, BUT without Jquery
    $(a).closest(".parent").find(".divA").hide();
}
like image 347
Marcelo Assis Avatar asked Dec 27 '22 08:12

Marcelo Assis


1 Answers

That's what parentNode is for:

a.parentNode.querySelectorAll('.divA');
like image 103
Joseph Silber Avatar answered Jan 09 '23 20:01

Joseph Silber