<?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: RTSJesp95</title>
    <description>The latest articles on DEV Community by RTSJesp95 (@rtsjesp95).</description>
    <link>https://dev.to/rtsjesp95</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%2F223031%2F47223d06-e4e1-4f56-bc4a-d995c200e263.png</url>
      <title>DEV Community: RTSJesp95</title>
      <link>https://dev.to/rtsjesp95</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rtsjesp95"/>
    <language>en</language>
    <item>
      <title>Click Function for Beginners</title>
      <dc:creator>RTSJesp95</dc:creator>
      <pubDate>Tue, 03 Sep 2019 11:39:55 +0000</pubDate>
      <link>https://dev.to/rtsjesp95/click-function-for-beginners-a71</link>
      <guid>https://dev.to/rtsjesp95/click-function-for-beginners-a71</guid>
      <description>&lt;p&gt;In this tutorial, I'm gonna teach you about the click function. Just a disclaimer, I'm only gonna go over the basics of using the Click Funtion as it is a very wide subject to go in-depth.&lt;/p&gt;

&lt;p&gt;Also a small note: &lt;/p&gt;

&lt;p&gt;You don't have to give the IDs, classes and variables the same names as I do. You can name them whatever you want, I just chose to keep my names simple&lt;/p&gt;

&lt;p&gt;First things first, set up a HTML Document in a code editor (I use Visual Studio Code) to have 3 buttons inside the Body and give them an ID:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button id="button1"&amp;gt;Button 1&amp;lt;/button&amp;gt;

&amp;lt;button id="button2"&amp;gt;Button 2&amp;lt;/button&amp;gt;

&amp;lt;button id="button3"&amp;gt;Button 3&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prepare a JS document and create variables for all the buttons. Like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener('DOMContentLoaded', ()=&amp;gt;{
    let button1 = document.querySelector('#button1');
    let button2 = document.querySelector('#button2');
    let button3 = document.querySelector('#button3');
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Adding the Click Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now you can add the click function to one of the buttons with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button1.addEventListener('click', ()=&amp;gt;{
    console.log('You clicked Button 1')
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The reason for a console.log, is to be sure that you have the right element clicked on.&lt;/p&gt;

&lt;p&gt;And now we can add it to the 2 remaining Buttons&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button2.addEventListener('click', ()=&amp;gt;{
    console.log('You clicked Button 2')
})

button3.addEventListener('click', ()=&amp;gt;{
    console.log('You clicked Button 3')
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Making the buttons change Background Color&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make it change Background Color, do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button2.addEventListener('click', ()=&amp;gt;{
    button2.style.backgroundColor = 'blue'
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This should change Button 2's Background Color to Blue&lt;/p&gt;

&lt;p&gt;Now let's say you want to change the color back to the original color. Do the following by creating a variabel after your Buttons, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let button1 = document.querySelector('#button1');
let button2 = document.querySelector('#button2');
let button3 = document.querySelector('#button3');
let isColored = false;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The reason for creating the isColored variabel, is something to have, so we can check if the button color changed&lt;/p&gt;

&lt;p&gt;Now inside the Click Function add an if and else statment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button2.addEventListener('click', ()=&amp;gt;{
    if(isColored){
        button2.style.backgroundColor = 'buttonface'
        isColored = false;        
    }else{
        button2.style.backgroundColor = 'green'
        isColored = true;
    }
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The if statement checks if the Button has changed color. Right now it's false, so it has the Default color for the button. If you click the button it should change the color to green. If it worked, the isColored variabel is now true, meaning the color has in fact changed. If you click the button again, it should revert back to the default color ('buttonface' is the default color value for a button element)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Working with classList.add/remove&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can also do the same with classList.add/remove. In this case would I like to add some padding and margin to one of the buttons and making the text bold. Gonna create a 4th Button for this example. Also gonna create a variable to keep track of the style, to see if it has have changed or not. Just so it doesn't mess with our other code. Let's take a look at the CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.padding-margin-bold{
    padding: 10px;
    border-radius: 15px;
    margin: 10px;
    font-weight: bold;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also feel free call your class whatever you like.&lt;/p&gt;

&lt;p&gt;I've added some padding, margin, border-radius and font-weight. Now to add this class via JavaScript. Let's move on over to the JavaScript file. Make sure to have created a variabel to point at the 4th button you just created in HTML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let styleChanged = false;
let button4 = document.querySelector('#button4');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have used some of the same code from the example before when changing the background color, instead I'm using classList instead of style. Here is the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button4.addEventListener('click', ()=&amp;gt;{
    if(styleChanged){
        button4.classList.remove("padding-margin-bold")
        styleChanged = false;        
    }else{
        button4.classList.add("padding-margin-bold")
        styleChanged = true;
    }
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now the class should be added when you click on the button and remove the class when you click on it again&lt;/p&gt;

&lt;p&gt;Thank you for reading through my tutorial :)&lt;/p&gt;

&lt;p&gt;Hope it helped you getting better at understanding Click Functions&lt;/p&gt;

&lt;p&gt;Here is a link to CodePen for the full example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/jesp258/pen/VwZMeBR"&gt;https://codepen.io/jesp258/pen/VwZMeBR&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Small note:&lt;/p&gt;

&lt;p&gt;In the CodePen example above, I have unique 'false' variables for all the buttons so they don't mess with each other in the code&lt;/p&gt;

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