How to trigger event in javascript

How to call any event in pure javascript, including custom, if possible?

In jQuery this can be done using .trigger(), but I want to understand the basis.
I would be grateful for links that will help to understand the mechanism of events in pure js, then to have over its implementation of triggers in jQuery to think.
October 8th 19 at 02:24
5 answers
October 8th 19 at 02:26
Works, I checked the current of events, two, these, and a mouse, ideally, they should be divided - aniya.Kertzmann65 commented on October 8th 19 at 02:29
October 8th 19 at 02:28
In all modern browsers you can create an event using the designer
document.documentElement.addEventListener("click", function(e) {
 console.log(e.type, e);
})
document.documentElement.addEventListener("magic", function(e) {
 console.log(e.type, e);
})

var event = new Event("click", {bubbles : true, cancelable : true})
var customEvent = new CustomEvent("magic", {bubbles : true, cancelable : true, detail : "<any string data>"})

document.documentElement.dispatchEvent(event);
document.documentElement.dispatchEvent(customEvent);
</any>


Compatibility with older browsers, you can ensure that using polyfill libraries
October 8th 19 at 02:30
Here's another:
xpoint.ru/forums/programming/javascript/misc/thread/34487.xhtml

IE: fireEvent(),
rest: document.createEvent()

If I understand correctly, the custom event framework-specific.
In addition, you can dig yourself jQuery (the code is not too simple, but annotated by):
github.com/jquery/jquery/blob/8d94ed91e47f68f742b6c282c8de40eb5fa8e140/src/event.js

In addition, there is a wonderful book by David Flanagan "Javascript. Detailed guide" in the network somewhere pdf me events devoted an entire section (50 pages) :-)
Yes, there is a great reference at the end there, the book is a must-have - aniya.Kertzmann65 commented on October 8th 19 at 02:33
October 8th 19 at 02:32
The idea of this. There is a way for w3c browsers, for IE. In the w3c called one function to create event object, it populates desired deneyimi. and the second function, which starts the proliferation of these events, i.e. calls listening to his handlers. In Ie it seems easier, everything is done by one function.

Dirty tricks: in IE, you cannot create custom event, only standard. Also, the launch of events like, click, only handlers onclick(), but do not press the button, do not move the cursor in the input field, it.d., that is, does the same as a real mouse click.

For more details, look for the words fireEvent, createEvent, dispatchEvent at sites MDN and MSDN.
October 8th 19 at 02:34
I do so:

var obj = document.getElementById ("obj_name");<br> if (obj.addEventListener) { <br> // All browsers except IE<br> obj.addEventListener("mousedown", function_name, false);<br> } else if (obj.attachEvent) {<br> // Internet Explorer and Opera<br> val1.attachEvent ("onmousedown", function_name)<br> }<br> function function_name() {<br> alert("the Event "onmousedown" was applied to the element with ID = "obj_name" ");<br> }<br>

Find more questions by tags JavaScript