<?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: Laurent</title>
    <description>The latest articles on DEV Community by Laurent (@laurentcodes).</description>
    <link>https://dev.to/laurentcodes</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%2F134488%2F5802c1d6-52f6-42d9-84fb-2dcf05e5c40d.png</url>
      <title>DEV Community: Laurent</title>
      <link>https://dev.to/laurentcodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/laurentcodes"/>
    <language>en</language>
    <item>
      <title>Event Delegation with JavaScript (In Simple Terms)</title>
      <dc:creator>Laurent</dc:creator>
      <pubDate>Mon, 01 Jun 2020 15:15:57 +0000</pubDate>
      <link>https://dev.to/laurentcodes/event-delegation-with-javascript-3oo4</link>
      <guid>https://dev.to/laurentcodes/event-delegation-with-javascript-3oo4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Event Delegation&lt;/strong&gt; can be said to be a way of adding event listeners where you add a single event handler to a parent element in order to avoid having to add event handlers to multiple child elements.&lt;/p&gt;

&lt;p&gt;This is particularly useful when we are dealing with elements that are dynamically added to the DOM, i.e they are not present when the page is initially loaded.&lt;/p&gt;

&lt;p&gt;I recently worked on a project where I had to add list items to a page from a script, and each item needed some interaction, after trying to just target each list item unsuccessfully I decided to make a research and then I saw event delegation (mind you, I have come across this in a course before, but it didn't stick).&lt;/p&gt;

&lt;p&gt;Anyways i started playing around with the concept and eventually got a hang of the terms and methodology and so I want to put it into writing here.&lt;/p&gt;

&lt;p&gt;Using the below example: I have to access &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; tags in a &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; tag with JavaScript and each &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; needs an event listener for the'click', event.&lt;br&gt;
What I did was add the event listener to the &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So:&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="nx"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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;What this does is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The event listener is added to the ul element.&lt;/li&gt;
&lt;li&gt;An event parameter is passed in &lt;code&gt;(e)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Using the &lt;code&gt;e&lt;/code&gt; parameter we can now access the target element (i.e the exact element being clicked in this case using e.target.&lt;/li&gt;
&lt;li&gt;When each &lt;code&gt;li&lt;/code&gt; is clicked we get different values for e.target, this is to show that each &lt;code&gt;li&lt;/code&gt; has it's own event listener now and we can do what we want with them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I tried to make this as simple enough for beginners like me so I really hope it is understandable.&lt;/p&gt;

&lt;p&gt;`&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>events</category>
      <category>vanillajs</category>
    </item>
  </channel>
</rss>
