<?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: Wazir babikir</title>
    <description>The latest articles on DEV Community by Wazir babikir (@wazirbabikir).</description>
    <link>https://dev.to/wazirbabikir</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F830882%2F0ddf2b3f-7977-43fc-a71b-b43c9f2e4367.jpeg</url>
      <title>DEV Community: Wazir babikir</title>
      <link>https://dev.to/wazirbabikir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wazirbabikir"/>
    <language>en</language>
    <item>
      <title>HOW TO DEMONSTRATE EVENTS HANDLERS IN DOM WITH JS.</title>
      <dc:creator>Wazir babikir</dc:creator>
      <pubDate>Tue, 15 Mar 2022 09:13:54 +0000</pubDate>
      <link>https://dev.to/wazirbabikir/how-to-demonstrate-events-handlers-in-dom-with-js-2e4a</link>
      <guid>https://dev.to/wazirbabikir/how-to-demonstrate-events-handlers-in-dom-with-js-2e4a</guid>
      <description>&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;WHAT IS DOM&lt;/strong&gt;&lt;br&gt;
The Document Object Model is a cross-platform and language-independent interface that treats an XML or HTML document as a tree structure wherein each node is an object representing a part of the document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;TYPE OF EVENTS HANDLERS&lt;/strong&gt;&lt;br&gt;
An event is a signal that something has happened. All DOM nodes generate such&lt;br&gt;
signals (but events are not limited to DOM).&lt;br&gt;
Here’s a list of the most useful DOM events, just to take a look at:&lt;br&gt;
&lt;strong&gt;Mouse events:&lt;/strong&gt;&lt;br&gt;
click – when the mouse clicks on an element (touchscreen devices generate it&lt;br&gt;
on a tap).&lt;br&gt;
contextmenu – when the mouse right-clicks on an element.&lt;br&gt;
mouseover / mouseout – when the mouse cursor comes over / leaves an&lt;br&gt;
element.&lt;br&gt;
mousedown / mouseup – when the mouse button is pressed / released over an&lt;br&gt;
element.&lt;br&gt;
mousemove – when the mouse is moved.&lt;br&gt;
&lt;strong&gt;Keyboard events:&lt;/strong&gt;&lt;br&gt;
keydown and keyup – when a keyboard key is pressed and released.&lt;br&gt;
&lt;strong&gt;Form element events:&lt;/strong&gt;&lt;br&gt;
submit – when the visitor submits a &lt;/p&gt; .
focus – when the visitor focuses on an element, e.g. on an  .
&lt;strong&gt;Document events:&lt;/strong&gt;
DOMContentLoaded – when the HTML is loaded and processed, DOM is fully
built.
&lt;strong&gt;CSS events:&lt;/strong&gt;
transitionend – when a CSS-animation finishes.
There are many other events. We’ll get into more details of particular events in next
3.** HOW TO LISTEN TO EVENTS**
To listen for all events on an element with JavaScript, we can loop through the keys of the window object to see which property starts with on .&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then we can take the part of the key after on as the event name and call addEventListener with it to listen to the event.&lt;/p&gt;

&lt;p&gt;For instance, we can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object.keys(window).forEach(key =&amp;gt; {  
  if (/^on/.test(key)) {  
    window.addEventListener(key.slice(2), event =&amp;gt; {  
      console.log(event);  
    });  
  }  
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;*&lt;em&gt;WE BULIT A REAL FORM TO PRINT USERNAME, PASSWORD AND EMAIL:
*&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 &amp;lt;h1 class="text-center"&amp;gt;REGISTRATION FORM&amp;lt;/h1&amp;gt;
    &amp;lt;form class="jumbotron container"&amp;gt;
        &amp;lt;div class="form-group"&amp;gt;
          &amp;lt;label for="exampleFormControlInput1"&amp;gt;USERNAME&amp;lt;/label&amp;gt;
          &amp;lt;input type="text" class="form-control username" placeholder="enter username"&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="form-group"&amp;gt;
            &amp;lt;label for="exampleFormControlInput1"&amp;gt;PASSWORD&amp;lt;/label&amp;gt;
            &amp;lt;input type="text" class="form-control password"  placeholder="enter password"&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;div class="form-group"&amp;gt;
            &amp;lt;label for="exampleFormControlInput1"&amp;gt;EMAIL&amp;lt;/label&amp;gt;
            &amp;lt;input type="number" class="form-control email" placeholder="enter email"&amp;gt;
          &amp;lt;/div&amp;gt;
          &amp;lt;p class="results"&amp;gt;USERNAME:&amp;lt;/p&amp;gt;
          &amp;lt;p class="result1"&amp;gt;PASSWORD:&amp;lt;/p&amp;gt;
          &amp;lt;p class="result2"&amp;gt;EMAIL:&amp;lt;/p&amp;gt;

        &amp;lt;form&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the results of the form is as folows:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QOxp8QPF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mizikhqpb28b6xluiief.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QOxp8QPF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mizikhqpb28b6xluiief.PNG" alt="Image description" width="880" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the code of the event listener of the form&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The second step is to do grab the elements from the HTML&lt;/strong&gt;&lt;br&gt;
To grab an HTML element we will use document.querySelector(), or we can use document.getElementBy... As for querySelector, its a method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// grap the elements(1)
      const username = document.querySelector('.username')
      const password = document.querySelector('.password')
      const email= document.querySelector('.email')

      const result = document.querySelector('.results')
      const result1 = document.querySelector('.result1')
      const result2 = document.querySelector('.result2')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After grabbing the elements we add the eventListners, and the function target.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//add event listeners
    username.addEventListener('input',displayvalue)
    password.addEventListener('input',displayvalue)
    email.addEventListener('input',displayvalue)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;reason for adding eventlisteners after grabbing the element...&lt;br&gt;
The addEventListener() method makes it easier to control how the event reacts to bubbling. When using the addEventListener() method, the JavaScript is separated from the HTML markup, for better readability and allows you to add event listeners even when you do not control the HTML markup.&lt;/p&gt;

&lt;p&gt;difference of using input and on change in the events listeners...&lt;br&gt;
The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus, after the content has been changed.&lt;/p&gt;

&lt;p&gt;after adding the events listeners we create functions&lt;br&gt;
&lt;strong&gt;A JavaScript function:&lt;/strong&gt; is a set of code that is written once but is executed any number of times. The functions eliminate code complexity by enabling re-usability. A function is a block of 'reusable code' that is used several times in the JavaScript program where it is defined.&lt;br&gt;
&lt;strong&gt;How to Create a Function in JavaScript&lt;/strong&gt;&lt;br&gt;
1.Use the keyword function followed by the name of the function.&lt;br&gt;
2.After the function name, open and close parentheses.&lt;br&gt;
3.After parenthesis, open and close curly braces.&lt;br&gt;
4.Within curly braces, write your lines of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// create the function(2)
function displayvalue() {
    const myusername = username.value
    result.innerText = myusername;
    const mypassword = password.value
    result1.innerText = mypassword;
    const myemail = email.value
    result2.innerText = myemail
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
