Coder Social home page Coder Social logo

fewpjs-event-listening-recoded-sanaa-2019's Introduction

Interacting with the DOM via JavaScript Event Listeners

Learning Goals

  • Demonstrate triggering event listeners on DOM nodes with addEventListener()

Introduction

We know how to manipulate nodes in the DOM by using JavaScript. We also know that events can be handled and can do work inside callback functions when the event is triggered. JavaScript programmers often say we are "listening" for an event in order to "execute" or "call" a callback function.

In order to teach nodes (or, "elements") to "listen" for an event, we use addEventListener(). It allows us to associate "hearing" an event with executing a callback.

Demonstrate Defining Event Listeners on DOM Nodes with addEventListener()

addEventListener() takes two arguments:

  1. the name of the event
  2. a callback function to "handle" the event

Take a look at index.html in the browser. When you click in the <input> area, nothing happens. Now let's set up some event handling.

Start by adding an event listener for the click event on the input#input element in index.html.

Try out the following in the Chrome DevTools console:

const input = document.getElementById('input');
input.addEventListener('click', function(event) {
  alert('I was clicked!');
});

When you click inside of input#input, now, you will get an alert box.

Take this code and paste it into the index.js file's addingEventListener function.

Let's review the parts of this code:

  1. The node that will be doing the listening. We store that node in the input constant
  2. The invocation of addEventListener on the node that will be doing the listening
  3. The first argument is the event name to listen for
  4. The second argument is the callback function. It's work that will be executed when the node "hears" the event

Conclusion

When you have pasted the solution into the addingEventListener function, your tests should pass when you run learn. This means you can run learn submit and move to the next lesson!

Resources

View Interacting with the DOM via JavaScript Event Listeners on Learn.co and start learning to code for free.

fewpjs-event-listening-recoded-sanaa-2019's People

Contributors

drakeltheryuujin avatar jenmyers avatar maxwellbenton avatar mustafa-al-shaheri avatar sgharms avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.