Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript closest child

// Html part
<article>
  <div id="div-01">Here is div-01
    <div id="div-02">Here is div-02
      <div id="div-03">Here is div-03</div>
    </div>
  </div>
</article>

// Js code to get closest
var el = document.getElementById('div-03');

var r1 = el.closest("#div-02");
// returns the element with the id=div-02

var r2 = el.closest("div div");
// returns the closest ancestor which is a div in div, here it is the div-03 itself

var r3 = el.closest("article > div");
// returns the closest ancestor which is a div and has a parent article, here it is the div-01

var r4 = el.closest(":not(div)");
// returns the closest ancestor which is not a div, here it is the outmost article
Comment

PREVIOUS NEXT
Code Example
Javascript :: Is Even 
Javascript :: join string js with and at the last item 
Javascript :: relation between leaves nodes and internal nodes in binary tree 
Javascript :: expression javascript 
Javascript :: how to create a search engine with javascript 
Javascript :: jquery view image in codeigniter 
Javascript :: bounce of two circles javascript 
Javascript :: mock anonymous function jest 
Javascript :: windows 10 retiré le theme sombre explorateur 
Javascript :: @hapi/disinfect 
Javascript :: last underscore 
Python :: colab mount drive 
Python :: shebang for python linux 
Python :: rotate axis labels matplotlib 
Python :: python show all columns 
Python :: get the current year in python 
Python :: pandas convert string from INT TO str 
Python :: drop a column pandas 
Python :: python get script name 
Python :: create python alias for python3 
Python :: conda create environment python 3.6 
Python :: mp4 get all images frame by frame python 
Python :: how to feature selection in python 
Python :: split data validation python 
Python :: django no such table 
Python :: plot image without axes python 
Python :: python click on screen 
Python :: not x axis labels python 
Python :: How to convert number string or fraction to float 
Python :: blender python set object to active by name 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =