<?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>Forem: Mark "niteCoda" Freeman</title>
    <description>The latest articles on Forem by Mark "niteCoda" Freeman (@pugcondoin).</description>
    <link>https://forem.com/pugcondoin</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%2F567676%2F1561362c-3a89-48c5-9bf0-b9cd18a4ff4e.jpeg</url>
      <title>Forem: Mark "niteCoda" Freeman</title>
      <link>https://forem.com/pugcondoin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pugcondoin"/>
    <language>en</language>
    <item>
      <title>A Tricky Case of insertAdjacentHTML()</title>
      <dc:creator>Mark "niteCoda" Freeman</dc:creator>
      <pubDate>Wed, 27 Jan 2021 15:02:32 +0000</pubDate>
      <link>https://forem.com/pugcondoin/a-tricky-case-of-insertadjacenthtml-141f</link>
      <guid>https://forem.com/pugcondoin/a-tricky-case-of-insertadjacenthtml-141f</guid>
      <description>&lt;h1&gt;
  
  
  How to insert multiple adjacent JavaScript strings as HTML
&lt;/h1&gt;

&lt;p&gt;As part of my commitment to &lt;a href="https://twitter.com/nitecoda1"&gt;#100HoursOfJavaScript&lt;/a&gt; study with &lt;a class="comment-mentioned-user" href="https://dev.to/wesbos"&gt;@wesbos&lt;/a&gt;
 &lt;a href="https://beginnerjavascript.com/"&gt;Beginner JavaScript&lt;/a&gt;, I stumbled upon a tricky challenge as part of the DOM Cardio - a series of small tests designed as a JavaScript workout. &lt;em&gt;It really is!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I'm obviously a JavaScript beginner and this seemingly simple challenge really had me stumped on several attempts. It may be simple to many and there may be several ways to solve it. In any case, I thought I'd share my solution with a little explanation. I hope it helps you get out of a pinch too and I'd be pleased to see how you might go about resolving in your own way.&lt;/p&gt;

&lt;p&gt;The overall challenge, using JavaScript alone, is to create a &lt;em&gt;wrapper div&lt;/em&gt; into which we'll add &lt;em&gt;two child divs&lt;/em&gt;. One containing an &lt;em&gt;unordered list of three items&lt;/em&gt;, the other, &lt;em&gt;two paragraphs&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Breaking down the challenge
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Create a &lt;code&gt;div&lt;/code&gt; with the class of &lt;code&gt;wrapper&lt;/code&gt; and add it to the document body:
&lt;/h3&gt;

&lt;p&gt;This first part shouldn't be too much trouble and here are the three lines of code we'll use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const wrapper = document.createElement('div');
wrapper.classList.add('wrapper');
document.body.appendChild(wrapper);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Make an unordered list containing three list items, and place this list inside the wrapper.
&lt;/h3&gt;

&lt;p&gt;This next step isn't too tricky either. We'll make use of backticks to make creation quick and easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const list = `
  &amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;list item one&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;list item two&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;list item three&amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we'll add those contents inside the wrapper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wrapper.innerHTML = (list);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Using an HTML string, create another div with two paragraphs inside of it.
&lt;/h3&gt;

&lt;p&gt;We already know how to do this, repeat the process above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const div = `
  &amp;lt;div class="paragraphs"&amp;gt;
    &amp;lt;p&amp;gt;Paragraph One&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;Paragraph Two&amp;lt;/p&amp;gt;
  &amp;lt;/div&amp;gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Now insert this new div &lt;em&gt;before&lt;/em&gt; the unordered list from above.
&lt;/h3&gt;

&lt;p&gt;And this is where things got tricky for me..........&lt;/p&gt;

&lt;p&gt;We now need to insert the variable div (with the two paragraphs) adjacent to (before) the variable list. My go to solution was to use &lt;code&gt;insertAdjacentHTML()&lt;/code&gt;, but that won't work.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why?
&lt;/h4&gt;

&lt;p&gt;Because the &lt;code&gt;list&lt;/code&gt; content we created earlier in our code is a string, not HTML. That means we can't target it with this method...... yet.&lt;/p&gt;

&lt;h4&gt;
  
  
  Then how?
&lt;/h4&gt;

&lt;p&gt;The way I went about solving this was by creating another variable &lt;code&gt;listElement&lt;/code&gt; using &lt;code&gt;querySelector()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const listElement = document.querySelector('ul');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;querySelector()&lt;/code&gt; method will target the &lt;code&gt;ul&lt;/code&gt; inside the list string and the new variable &lt;code&gt;listElement&lt;/code&gt; (we could have called it whatever we like) will hold valid HTML elements that will work alongside &lt;code&gt;insertAdjacentHTML()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now we can set about targeting the UL element, then placing our paragraphs div  alongside using insertAdjacentHTML().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listElement.insertAdjacentHTML('beforebegin', div);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;insertAdjacentHTML()&lt;/code&gt; takes in two parameters for the insertion point: ('&lt;strong&gt;location&lt;/strong&gt;' and the &lt;strong&gt;insertion&lt;/strong&gt;). We wanted to insert the paragraphs before the list so we used &lt;code&gt;beforebegin&lt;/code&gt; as our insertion point.&lt;/p&gt;

&lt;p&gt;The range of location options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;beforebegin&lt;/strong&gt; = before element&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;afterbegin&lt;/strong&gt; = before first child of element&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;beforeend&lt;/strong&gt; = after last child of element&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;afterend&lt;/strong&gt; = after element&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  LINKS
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://twitter.com/nitecoda1"&gt;@nitecoda&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML"&gt;MDN Web Docs insertAdjacentHTML&lt;/a&gt;&lt;br&gt;
&lt;a href="https://beginnerjavascript.com/"&gt;Beginner JavaScript from @wesbos&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Event Listeners for Beginners</title>
      <dc:creator>Mark "niteCoda" Freeman</dc:creator>
      <pubDate>Tue, 26 Jan 2021 14:33:12 +0000</pubDate>
      <link>https://forem.com/pugcondoin/javascript-event-listeners-for-beginners-2bm9</link>
      <guid>https://forem.com/pugcondoin/javascript-event-listeners-for-beginners-2bm9</guid>
      <description>&lt;h1&gt;
  
  
  What Are Events and Listeners?
&lt;/h1&gt;

&lt;p&gt;Visitors to our websites, and users of our applications, are continuously interacting with elements of the Document Object Module (DOM), whether they realise it or not. &lt;/p&gt;

&lt;p&gt;When these interactions occur, the elements themselves emit &lt;strong&gt;event actions&lt;/strong&gt;. As JavaScript developers, we're able to listen for these events using the JavaScript method for &lt;strong&gt;event listeners&lt;/strong&gt; and then handle them with appropriate and well-timed responses. Assuming that's our our aim, and it certainly should be.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Should We Listen for Element Events?
&lt;/h2&gt;

&lt;p&gt;Responding to user activity can greatly &lt;strong&gt;enhance user experience&lt;/strong&gt; and interactivity leading to &lt;strong&gt;improved engagement&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Events Exactly?
&lt;/h3&gt;

&lt;p&gt;Events vary according to element type, most are obvious but some not so. They include, for example; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mouse&lt;/strong&gt; events:- such as mouseover, mousedown, mouseup and click; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keyboard&lt;/strong&gt; events:- such as keydown and keyup; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Form&lt;/strong&gt; events:- such as focus and submit; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Window&lt;/strong&gt; events:- such as scroll and resize; &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;to name but a few&lt;/em&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A detailed explanation to writing your first simple event listener:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Step 1 - Target the element and declare a new variable.
&lt;/h4&gt;

&lt;p&gt;The very first step in preparing our code for listening and handling events, is to declare a variable for the element that will be emitting the event we're interested in. In the following simple example, that element is a button with the class &lt;code&gt;click-me&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We'll declare a variable with an intuitive name; &lt;code&gt;clickMe&lt;/code&gt; and use the button's class of &lt;code&gt;click-me&lt;/code&gt; to select the element from within the document, using the JavaScript method &lt;code&gt;.querySelector()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const clickMe = document.querySelector('.click-me');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Call the &lt;code&gt;click-me&lt;/code&gt; variable that we've just declared for the button:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;click-me
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Next, we attach to that variable, the JavaScript method for event listening &lt;code&gt;.addEventListener()&lt;/code&gt;.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;click-me.addEventListener();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The event listener takes in two parameters, comma-separated, inside the parenthesis:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- The first is the emit event which sets up the listener. In this example we'll listen for a &lt;strong&gt;click&lt;/strong&gt; event. &lt;/li&gt;
&lt;li&gt;- The second, a function that will be called to action when the specified event occurs. This function is known as a &lt;strong&gt;callback function&lt;/strong&gt; and in this example, it has no name and so, is anonymous.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  4. Add the event Parameters
&lt;/h4&gt;

&lt;p&gt;Our Parameters in this case are &lt;code&gt;click&lt;/code&gt;, and an empty function &lt;code&gt;function()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;click-me.addEventListener('click', function());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  5. Add a code block:
&lt;/h4&gt;

&lt;p&gt;Next We'll open up a code block &lt;code&gt;{ }&lt;/code&gt; which will contain the code for the action we wish to take place in &lt;strong&gt;response&lt;/strong&gt; to the event:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;click-me.addEventListener('click', function() {  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  6. Add a callback response
&lt;/h4&gt;

&lt;p&gt;Inside the code block, we'll simply output an appropriate message to the console using &lt;code&gt;console.log();&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;clickMe.addEventListener('click', function() {
  console.log(`the button with class 'click-me' button was clicked`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our event listener is now complete with response and feedback.&lt;/p&gt;

&lt;p&gt;This simple event listener will execute a log to the console every time the button is clicked. It will also work if the user focuses the button using tab, and presses the enter key.&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple way to remember event listeners
&lt;/h2&gt;

&lt;p&gt;If all of this seems too much to remember at first, a simple way to remember the role and key ingredients of an event listener is this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Get&lt;/strong&gt; &lt;code&gt;click-me&lt;/code&gt; button&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Listen&lt;/strong&gt; &lt;code&gt;click&lt;/code&gt; event&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do&lt;/strong&gt; &lt;code&gt;console.log&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hopefully, this will help you along your own path towards JavaScript mastery.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
