<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matt Pignatore</title>
    <description>The latest articles on DEV Community by Matt Pignatore (@mpaitgt).</description>
    <link>https://dev.to/mpaitgt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F562994%2Fcc862f4c-cd6b-4e21-b3ad-91c957639cc5.jpeg</url>
      <title>DEV Community: Matt Pignatore</title>
      <link>https://dev.to/mpaitgt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mpaitgt"/>
    <language>en</language>
    <item>
      <title>Is this element a descendant of another?</title>
      <dc:creator>Matt Pignatore</dc:creator>
      <pubDate>Tue, 19 Jan 2021 14:49:56 +0000</pubDate>
      <link>https://dev.to/mpaitgt/is-this-element-a-descendant-of-another-1cn2</link>
      <guid>https://dev.to/mpaitgt/is-this-element-a-descendant-of-another-1cn2</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;The Problem&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Recently I was charged with ironing out the functionality of a custom modal which houses a form. I had to be able to open the modal with a button, and close it one of two ways: click outside of the modal, or submit the form.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Thought Process&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Initially I thought it'd be simple! Just write some conditional logic, checking the class of each element in the form. If the event target doesn't have any of those classes, then the modal can be closed. If the submit button is targeted, close the form. But it wasn't this simple...&lt;/p&gt;

&lt;p&gt;Reason being that this project includes third-party framework - &lt;strong&gt;Select2&lt;/strong&gt; - which was generating a dropdown that was positioned absolutely on the page and was not a descendant of my modal! Now, I could have written some JavaScript to add class selectors to those elements on page load, but this seemed inefficient. That's when I decided I had to traverse the DOM a bit.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Solution&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I ended up writing a single function that took two arguments: the target element and the parent I need to check for. The function returns true is it's a descendant of the parent, false if not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isDescendant&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isChild&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/* 
  if the target element *is* the parent 
  I'm looking for, return true 
  */&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;isChild&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;isChild&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="cm"&gt;/* 
  this loop travels upward from the target element
  until it reaches the parent I'm searching for
  and returns true
  */&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;isChild&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;isChild&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="cm"&gt;/*
  if neither of the above cases occur, return false.
  The target element is not a descendant.
  */&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;isChild&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! This function was incredibly helpful in getting my form modal to function properly. Hopefully it can be a bit of help to you too!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
