<?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: Chase Anderson</title>
    <description>The latest articles on DEV Community by Chase Anderson (@chaseanderson2).</description>
    <link>https://dev.to/chaseanderson2</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%2F1615855%2Fb78fd382-33f2-41a6-a5c5-dc33963e178d.png</url>
      <title>DEV Community: Chase Anderson</title>
      <link>https://dev.to/chaseanderson2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chaseanderson2"/>
    <language>en</language>
    <item>
      <title>Event &amp; Event Listeners in JavaScript</title>
      <dc:creator>Chase Anderson</dc:creator>
      <pubDate>Mon, 16 Dec 2024 18:03:54 +0000</pubDate>
      <link>https://dev.to/chaseanderson2/event-event-listeners-in-javascript-1k03</link>
      <guid>https://dev.to/chaseanderson2/event-event-listeners-in-javascript-1k03</guid>
      <description>&lt;p&gt;As I near the end of my first phase in my Flatiron School program, I have learned the fundamentals of programming in JavaScript. But in this blog post, I will discuss my experience with my favorite subject yet: Events and Event Listeners.&lt;/p&gt;

&lt;p&gt;JavaScript can listen for specific things that can happen in the browser you are working on. There is a great multitude of things that JS can listen to. Some examples include a key press, mouse click, and a form submission, just to name a few of the most used ones. They also serve as a great way to implement asynchronous interaction, the ability for a page to respond to user input without interrupting processes or reloading the page. This is crucial to the function of a web app, in which features like that are expected. What I love the most about events is the fact that they can add a lot of interactivity to the web project you are working on.&lt;/p&gt;

&lt;p&gt;Event listeners are JavaScript functions that wait for a specific event to occur on a specified element. Once the event triggers, the event listener will execute a specific block of code (typically called the callback function). The most common way to attach an event listener to an element is to use the &lt;code&gt;addEventListener&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;I will use a personal example that I am currently using in my end-of-phase project. For my project, I have created a simple web app that fetches information from an API and displays it on a web page with interactive elements. &lt;/p&gt;

&lt;p&gt;I decided to use the free &lt;a href="https://www.amiiboapi.com/" rel="noopener noreferrer"&gt;Amiibo API&lt;/a&gt; to create a web app that can filter through all of the Yoshi Amiibos created throughout the years. I loved the idea of using this API, given how Yoshi is my favorite Nintendo character! &lt;/p&gt;

&lt;p&gt;Here is an example of how I made this dropdown menu that lets users filter through different Yoshi Amiibos using event listeners!&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotb9yizufuowwl8rpm1v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fotb9yizufuowwl8rpm1v.png" alt="Dropdown menu" width="413" height="650"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is what my code looks like for this specific function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      const amiiboContainer = document.getElementById('amiibo-container');
      const amiiboDropdown = document.getElementById('amiiboDropdown');
      const amiibos = data.amiibo;

      // Create an array of names for the dropdown search
      const amiiboNames = amiibos.map(amiibo =&amp;gt; amiibo.name);

      // Populate the dropdown with Amiibo names
      amiiboNames.forEach(name =&amp;gt; {
        const option = document.createElement('option');
        option.value = name;
        option.textContent = name;
        amiiboDropdown.appendChild(option);
      });

      // Display all Amiibos initially
      displayAmiibos(amiibos);

      // Add an event listener to filter Amiibos when the dropdown value changes
      amiiboDropdown.addEventListener('change', (e) =&amp;gt; {
        const selectedName = e.target.value;
        const filteredAmiibos = amiibos.filter(amiibo =&amp;gt; amiibo.name === selectedName );
        displayAmiibos(filteredAmiibos);
      });
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let me walk you through what is going on in this block of code.  &lt;/p&gt;

&lt;p&gt;The first thing I did was create an array for the Yoshi Amiibo names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const amiiboNames = amiibos.map(amiibo =&amp;gt; amiibo.name);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line has a map function that iterates through the items stored in the &lt;em&gt;amiibos&lt;/em&gt; array. For each &lt;em&gt;amiibo&lt;/em&gt; object, it will take the &lt;em&gt;name&lt;/em&gt; property and return a new array with all the Amiibo names.&lt;/p&gt;

&lt;p&gt;The next step was to populate the dropdown menu with the Amiibo names.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;amiiboNames.forEach(name =&amp;gt; {
  const option = document.createElement('option');
  option.value = name;
  option.textContent = name;
  amiiboDropdown.appendChild(option);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This block creates an HTML &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; dropdown and fills it with the name of the Amiibos. It iterates over the &lt;code&gt;amiiboNames&lt;/code&gt; array using &lt;code&gt;forEach()&lt;/code&gt;, then creates an &lt;code&gt;&amp;lt;option&amp;gt;&lt;/code&gt; element for each individual Amiibo name.&lt;/p&gt;

&lt;p&gt;For each of the names, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates a new &lt;code&gt;option&lt;/code&gt; element.&lt;/li&gt;
&lt;li&gt;Sets the &lt;code&gt;value&lt;/code&gt; attribute of the &lt;code&gt;option&lt;/code&gt; to the Amiibo name.&lt;/li&gt;
&lt;li&gt;Sets the visible text (&lt;code&gt;textContent&lt;/code&gt;) of the &lt;code&gt;option&lt;/code&gt; to the Amiibo name.&lt;/li&gt;
&lt;li&gt;Appends our new &lt;code&gt;option&lt;/code&gt; element to the dropdown (&lt;code&gt;amiiboDropdown&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After that, I decided that I would want all of the Yoshi Amiibos to display initially before using the filter dropdown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;displayAmiibos(amiibos);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created this function to display all of the Amiibos on the page whenever the page loads. What this does is pass the entire &lt;code&gt;amiibos&lt;/code&gt; array into the function I just created above.&lt;/p&gt;

&lt;p&gt;Finally, I had to set up the event listener for the dropdown changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;amiiboDropdown.addEventListener('change', (e) =&amp;gt; {
  const selectedName = e.target.value;
  const filteredAmiibos = amiibos.filter(amiibo =&amp;gt; amiibo.name === selectedName);
  displayAmiibos(filteredAmiibos);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the most important parts of the code and is the event listener of this function. This is what allows the page to filter the Amiibos that are displayed on the page based on what is selected in the dropdown menu.&lt;/p&gt;

&lt;p&gt;The way that this works is that the &lt;code&gt;addEventListener()&lt;/code&gt; function listens for the &lt;code&gt;change&lt;/code&gt; event on the &lt;code&gt;amiiboDropdown&lt;/code&gt; element (or the &lt;code&gt;&amp;lt;select&amp;gt;&lt;/code&gt; dropdown). Whenever the user of the web app selects a new value, or in this case, a specific Yoshi Amiibo, the event will trigger the callback function.&lt;/p&gt;

&lt;p&gt;The order in which this works is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;selectedName&lt;/code&gt; variable will store the value of whatever option is selected with &lt;code&gt;e.target.value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;It will then filter the &lt;code&gt;amiibos&lt;/code&gt; array to make sure it will only include the Yoshi Amiibo that matches with the &lt;code&gt;selectedName&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After filtering, the &lt;code&gt;filteredAmiibos&lt;/code&gt; array is then passed to &lt;code&gt;displayAmiibos()&lt;/code&gt;, which will then update the UI with the filtered list of Yoshi Amiibos.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Events and Event Listeners in JavaScript are some of the most important things to know when developing a web app with interactivity. They are great because of that fact alone; it gives an app function and purpose, while giving users the opportunity for a seamless experience.&lt;/p&gt;

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