Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University

Slides:



Advertisements
Similar presentations
Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Advertisements

JavaScript Basics Course Introduction SoftUni Team Technical Trainers Software University
Software Quality Assurance QA Engineering, Testing, Bug Tracking, Test Automation Software University Technical Trainers SoftUni Team.
 Dimitar Ivanov Introduction to programming with microcontrollers.
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
Methods Writing and using methods, overloads, ref, out SoftUni Team Technical Trainers Software University
Software University Curriculum, Courses, Exams, Jobs SoftUni Team Technical Trainers Software University
Fundamentals SoftUni Welcome to Software University SoftUni Team Technical Trainers Software University
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC SoftUni Team Technical Trainers Software University
AngularJS Directives Defining Custom Directives SoftUni Team Technical Trainers Software University
Software Testing Lifecycle Exit Criteria Evaluation, Continuous Integration Ivan Yonkov Technical Trainer Software University.
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
JavaScript Design Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni.
NoSQL Databases NoSQL Concepts SoftUni Team Technical Trainers Software University
DOM and Events Document Object Model (DOM) Events Handling in JavaScript Svetlin Nakov Technical Trainer Software University
Composer packages Installing and Using Composer, Packagist, Packaging your code Mario Peshev Technical Trainer Software University
Start Your Own Blog Angel Georgiev Part-time Trainer angeru.softuni-friends.org Software University The Culture of Knowledge Sharing.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Database APIs and Wrappers
Entity Framework Performance SoftUni Team Technical Trainers Software University
Svetlin Nakov Technical Trainer Software University
Build Processes and Continuous Integration Automating Build Processes Software University Technical Trainers SoftUni Team.
Processing Redis with.NET How to Operate with Redis Databases SoftUni Team Technical Trainers Software University
Multidimensional Arrays, Sets, Dictionaries Processing Matrices, Multidimensional Arrays, Dictionaries, Sets SoftUni Team Technical Trainers Software University.
Project Tracking Tools Trello, Asana, Basecamp, GitHub Issue Tracker, TRAC Angel Georgiev Part-time Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding SoftUni Team Technical Trainers Software University
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
CV Structure, Content, Layout, Formatting, Best Practices SoftUni Team Technical Trainers Software University
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Jekyll Static Site Generator Template-Based Site Generation Svetlin Nakov Technical Trainer Software University
HTML Forms Forms, Controls, Fields, Inputs, Submission, Validation SoftUni Team Technical Trainers Software University
Forms Overview, Query string, Submitting arrays, PHP & HTML, Input types, Redirecting the user Mario Peshev Technical Trainer Software.
JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
Web Development Tools Tools for Front-End Developers Writing HTML and CSS Code SoftUni Team Technical Trainers Software University
Web Fundamentals (HTML and CSS)
JQuery Overview Unleash the Power of jQuery SoftUni Team Technical Trainers Software University
Responsive Design Design that Adapts to Different Devices SoftUni Team Technical Trainers Software University
Processing JSON in.NET JSON, JSON.NET LINQ-to-JSON and JSON to XML SoftUni Team Technical Trainers Software University
Tables, Rows, Columns, Cells, Header, Footer, Colspan, Rowspan
CSS Transitions and Animations Animated HTML Elements SoftUni Team Technical Trainers Software University
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
Mocking with Moq Tools for Easier Unit Testing SoftUni Team Technical Trainers Software University
Mocking Unit Testing Methods with External Dependencies SoftUni Team Technical Trainers Software University
Mocking with Moq Mocking tools for easier unit testing Svetlin Nakov Technical Trainer Software University
JavaScript Tools Tools for Writing / Editing / Debugging JavaScript Code Svetlin Nakov Technical Trainer Software University
Test-Driven Development Learn the "Test First" Approach to Coding Svetlin Nakov Technical Trainer Software University
Sets, Dictionaries SoftUni Team Technical Trainers Software University
JavaScript Applications Course Introduction SoftUni Team Technical Trainers Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Team Work and Personal Skills Course Introduction Angel Georgiev Part-time Trainer Software University
Creating Content Defining Topic, Creating Technical Training Materials SoftUni Team Technical Trainers Software University
Web Storage and Cookies Cookies, Local and Session Storage SoftUni Team Technical Trainers Software University
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
AngularJS Best Practices High Quality SPA Applications SoftUni Team Technical Trainers Software University
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Extending functionality using Collections
JavaScript and Events.
JavaScript and Events CS Programming Languages for Web Applications
CHAPTER 6 EVENTS. CHAPTER 6 EVENTS WHAT IS AN EVENT?
JavaScript and Events CS Programming Languages for Web Applications
Presentation transcript:

Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University

2 1.JavaScript Event Model 2.Event Handler Registration 3.The “event” object 4.Capturing and bubbling events  Event chaining 5.Creating custom events Table of Contents

JavaScript Event Model

4  The DOM event model provides notifications for certain events  E.g. execute a JS function when a button is clicked  The DOM event model consists of events and event listeners attached to the DOM objects  Events Demo Events Demo Event Model

5  DOM provides access to many events  Mouse events – mouse clicks, mouse moves, mouse over, …  Touch events – finger touch, touch start, end, move, …  Form events – field focus, value change, form submit, …  Keyboard events – key down, key up, key press, …  DOM / UI events – node insert, node remove, load, resize, …  Full list of all DOM event types:   You may also define custom event types Event Types

6 Common Event Types load abort select resize change click hover mouseup mousedown mouseover mouseout keydown keypress keyup focus blur focusin focusout  Mouse events  DOM / UI events  Keyboard events  Focus events touchstart touchend touchcancel touchleave touchmove  Touch events

Event Handler Registration

8  Event handling JavaScript code can be specified in the HTML attributes onclick, onload, onmouseover, onresize, … Define Event Handler in the HTML Code Click Me! Click Me! function buttonClickFunction() { console.log("You clicked the [Click Me!] button"); console.log("You clicked the [Click Me!] button");} OK OK

9  Event handling JavaScript code can be specified in the JS code through the properties onclick, onresize, … Define Event Handler in the JS Code Click Me! Click Me! Click me Click me var button = document.getElementById("click-button"); button.onclick = function onButtonClick() { console.log("You clicked the button"); console.log("You clicked the button");}

10  A more powerful way for attaching event handlers:  isCaptureEvent : catch the "capture" or "bubbling" phase  Can attach multiple events in a chain Using addEventListener(…) domElement.addEventListener( eventType, eventHandler, isCaptureEvent) eventType, eventHandler, isCaptureEvent) var button = document.getElementById("buttonOK"); button.addEventListener("click", function() { console.log("You clicked me"); console.log("You clicked me"); }, false);

Event Handlers Live Demo

The "event" Object Just take it

13  The " event " object holds information about the event  Passed as parameter to the event handling function  The event object contains information about:  The type of the event (e.g. 'click', 'resize', …)  The target of the event (e.g. the button clicked)  The key pressed for keyboard events  The mouse button / cursor position for mouse events The "event" Object btn.onclick = function(event) { alert(event); }

14 Event Object  The " event " object is the only argument of the event handler  Old IE versions pass the event in window.event function onButtonClick(event) { console.log(event.target); console.log(event.type); console.log("(" + event.clientX + ", " + event.clientY + ")"); } button.addEventListener("click", onButtonClick, false); function onButtonClick(event) { if (!event) event = window.event; if (!event) event = window.event; // Your event handling code … // Your event handling code …}

The "event" Object Live Demo

Capturing and Bubbling Events

17  When the user clicks on an HTML element  E.g. on a button in the page  The event is also fired on all of its parents  The button is still the target  But the click event is fired on all of its parents  The click event is fired on all elements in the chain Browser Events Chain

 Capturing handlers go down the chain  The first executed handler is on the parent  The last executed handler is on the target  Bubbling handlers bubble up to the parent  The first executed handler is on the target  Then its parent's, and its parent's, etc.  Capturing and Bubbling Explained Capturing and Bubbling Explained Event Chains: Types domElement.addEventListener(eventType, eventHandler, isCaptureEvent) eventHandler, isCaptureEvent) 18

Event Chain Live Demo

Custom events Creating and dispatching custom events

 Using the JavaScript API we can create our own events  The CustomEvent class allows you to create your own events.  To create a custom event you need to call the constructor as follows: Creating custom events new CustomEvent(name, [customEventInit]); Optional: initialization parameters The name of the event

22  After we create the event we need to add a listener that listens for that event.  Finally we dispatch/trigger the event when needed. Creating custom events(2) var customEv = new CustomEvent('yell'); elem.addEventListener('yell', function(e) { … }); elem.dispatchEvent(customEv);

Custom events Live Demo

24 1.JavaScript Event Model 2.Event Handler Registration 3.The “event” object 4.Capturing and bubbling events  Event chaining 5.Creating custom events Summary

? ? ? ? ? ? ? ? ? JavaScript DOM and Events

License  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" licenseCreative Commons Attribution- NonCommercial-ShareAlike 4.0 International 26  Attribution: this work may contain portions from  “JavaScript Basics" course by Telerik Academy under CC-BY-NC-SA licenseJavaScript BasicsCC-BY-NC-SA

Free Software University  Software University Foundation – softuni.orgsoftuni.org  Software University – High-Quality Education, Profession and Job for Software Developers  softuni.bg softuni.bg  Software Facebook  facebook.com/SoftwareUniversity facebook.com/SoftwareUniversity  Software YouTube  youtube.com/SoftwareUniversity youtube.com/SoftwareUniversity  Software University Forums – forum.softuni.bgforum.softuni.bg