Git Product home page Git Product logo

fewpjs-javascript-eventing's Introduction

How to Leverage JavaScript Events

Learning Goals

  • Define a JavaScript event
  • Identify different types of user events

Introduction

We've experimented with selecting and manipulating nodes in the DOM using JavaScript: deleting nodes, editing nodes, etc. But most web applications are not used by people opening up the console and editing the DOM using Chrome's DevTools :). Instead, people do something and then work happens.

"Doing work" in response to "something happening" is known as event handling. Events are the "something that happens" and the "callback function" is the work that will happen in response to the event being triggered. In this lesson we'll focus on the first half of that relationship: the event. We'll discuss the second half, the "callback function," or, "callback" in a later lesson.

We'll explore this relationship in this document.

Define a JavaScript Event

JavaScript has the ability to "listen" for things that happen inside the browser. It can listen for events like whether the browser resized, or whether someone clicked on a specific image on the screen. The event you're probably most familiar with is "click."

We'll cover a few more common types of events below.

Identify Different Types of User Events

Let's take a look at some of the more common events.

Mouse Click

The mouse / trackpad is a primary pointing device when working with browsers. In response to click, double-click, right-click, etc. we can do work like, changing the background of the document to a random color every time someone clicks on the page.

Key Press

While click events make up the majority of events you'll use, the keyboard is also an important source of events. We can listen for keypress, keydown, and keyup. When these events are handled, we can find out which keys were pressed (like if a space was hit to make the character jump over the hole).

Form Submission

HTML pages often use a submit button to submit a form to a server. When a user submits a form, the submit event is fired. An event handler here might pop up a thank you overlay, play a song, or provide some other sort of interactivity depending on what values were entered in the form.

List of Events

As you seek to build more complicated applications, you'll need to handle and trigger work on more events. Here's a list of a ton of browser events.

Conclusion

JavaScript allows us to trigger work when it detects events. Some other common events are scroll, mouseenter and mouseleave, focus, blur, and onchange. There are lots and lots of events - what events do popular websites handle? When JavaScript recognizes an event that applies to a "event handler" that has been set up, it will execute that "handler's" work, which is stored in a callback function. This might feel intimidating at first, but if you familiarize yourself with them all now, you can get the hang of using them before you end up needing them for a project task!

Resources

fewpjs-javascript-eventing's People

Contributors

drakeltheryuujin avatar sgharms avatar maxwellbenton avatar

Watchers

James Cloos 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.