Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between js events and DOM events?

Tags:

javascript

dom

I am trying to understand events(such as: click, keyPress...) in js. But when I studied online, I saw it mentioned a lot on 'DOM events'. So my question is js events the same as DOM events? If not, what is the difference?

like image 770
user2294256 Avatar asked Jun 25 '13 07:06

user2294256


People also ask

What are DOM events in JavaScript?

HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document. Events are normally used in combination with functions, and the function will not be executed before the event occurs (such as when a user clicks a button).

Are events part of DOM?

The Event interface represents an event which takes place in the DOM. An event can be triggered by the user action e.g. clicking the mouse button or tapping keyboard, or generated by APIs to represent the progress of an asynchronous task.

What is the difference between function and event in JavaScript?

An event is an identifier used within tools ( . on() , . emit() etc) to set and execute callbacks. Functions are reusable code.


1 Answers

DOM events fires when the DOM changes or interacts with user, and they are Javascript events. Please have a read all of them: http://www.w3schools.com/jsref/dom_obj_event.asp

Apart from DOM events, you can define your own event objects in Javascript and use the 'dispatchEvent' method to fire that event. For example:

var event = new Event('build');

// Listen for the event.
elem.addEventListener('build', function (e) { ... }, false);

// Dispatch the event.
elem.dispatchEvent(event);

In short, you can think of DOM events are native Javascript events that fires from DOM elements. While a Javascript event can be a DOM event or Custom event

like image 104
David Lin Avatar answered Sep 20 '22 01:09

David Lin