<?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: Arturo Salmeron</title>
    <description>The latest articles on DEV Community by Arturo Salmeron (@asalmeron3).</description>
    <link>https://dev.to/asalmeron3</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%2F100375%2Feea0278c-15c8-47c6-94c7-43246637d042.jpeg</url>
      <title>DEV Community: Arturo Salmeron</title>
      <link>https://dev.to/asalmeron3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asalmeron3"/>
    <language>en</language>
    <item>
      <title>Dynamically Made Button</title>
      <dc:creator>Arturo Salmeron</dc:creator>
      <pubDate>Tue, 11 Sep 2018 16:23:03 +0000</pubDate>
      <link>https://dev.to/asalmeron3/dynamically-made-button-4e22</link>
      <guid>https://dev.to/asalmeron3/dynamically-made-button-4e22</guid>
      <description>&lt;p&gt;For this post, I will try to explain dynamically created buttons as I understand the concept. The implementation of this code is done via an independently created library that is similar to jQuery but was created for the purpose of learning and understanding what a library is and how it can be used in our code. &lt;/p&gt;

&lt;p&gt;When you want to create a component that is not part of your original HTML, you manipulate the DOM in order to get that component to display. This component can be created as a result to the user doing a certain action, completing a form, and/or clicking on a certain button. In our example, we will create a button as a result of clicking on another button with the id #add.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4bm6atk7yw1ohhvujppc.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4bm6atk7yw1ohhvujppc.PNG" title="function makeOneNewButton with click event on #add button" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our DOM initially only has the #add button:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwtdrtnuaj0fk0u54que5.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwtdrtnuaj0fk0u54que5.PNG" title="add button on DOM" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After we click on the add button, we successfully add our new button on the page:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhneueabiawd6pf8fzb01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhneueabiawd6pf8fzb01.png" title="new button on DOM" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, lets make a function and run that function when we click on our new button with the id #myNewButton: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkplbudmf67smw37jrb1h.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkplbudmf67smw37jrb1h.PNG" title="testNewButton function" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbakrzv290i39x85pnfwx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbakrzv290i39x85pnfwx.PNG" title="associating new function to our new button" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the new lines of code, we expect the new button to console a message for us once we click on it. However, nothing shows up in the console:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fudef1ah8f3z03d3o6926.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fudef1ah8f3z03d3o6926.png" title="DOM after clicking on new button" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why didn't this work? Our code is written correctly but we did not see a new message in the console. This is a rather hard concept to grasp and it has to do with dynamically changing our DOM and with the concept of &lt;a href="https://javascript.info/bubbling-and-capturing" rel="noopener noreferrer"&gt;Bubbling and Capturing.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The way I think about it is that the new button was not in the initial code thus our function did not have a point of reference to attach our listener (the click event) to our new button. Rephrased: our testMyNewButton function could not be tied to #myNewButton because that button was not part of the initial html that loaded.&lt;/p&gt;

&lt;p&gt;So instead, lets add the function to something that was already there. We can association the function to clicking on the document, on the body, or any other component that was in the origin HTML. For this example, we will associate the function to clicking on the div with the .container class as this is the div we appended the new button to. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Feo476aa1rdkeu5fyyqcs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Feo476aa1rdkeu5fyyqcs.png" title="associating function to .container" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if we refresh our DOM and click on the new button, we get a new message in the console:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxjawlon09dd1e7tzmb7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxjawlon09dd1e7tzmb7d.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Seems to work well! But what if I click to the right of our new button? &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1aikck851htfwnchfv1g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1aikck851htfwnchfv1g.png" title="all of the .container div" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
Our console logs another message here too. This is consistent with our code. Remember, we associated the function to the entire .container div thus we expect the function to run upon clicking anywhere inside of .container :&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff3h8rlr6gmtptwaxwivv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ff3h8rlr6gmtptwaxwivv.png" title="clicked anywhere in .container" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To fine tune and solely associate the click event to our new button, lets add some conditions to our function in order to check on the id of the element the user clicked on. To start, lets create a variable that will store the id of the element we will click on. To do this, we will use our initial "submittedOrClickedEvent" parameter and grab the target. The target will be the inner most element where our click event originated from. If that element has an id, we will want to store it:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fk6e1i7qxn90xae35vf72.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fk6e1i7qxn90xae35vf72.PNG" title="id of our target element" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have a variable with an id of an element, we can use it to compare it with the id of our new button #myNewButton :&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4jdkndbogmf7cdwfzjr2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4jdkndbogmf7cdwfzjr2.PNG" title="conditions when clicking on .container" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we refresh our DOM and click on the new button and only our new button, we will see our function running and printing to the console: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F73ri045oddzbj8anh9gz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F73ri045oddzbj8anh9gz.png" title="clicked on new button" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dom</category>
      <category>function</category>
      <category>button</category>
      <category>dynamic</category>
    </item>
  </channel>
</rss>
