V
V
Viktor Chistyakov2018-02-05 20:06:34
JavaScript
Viktor Chistyakov, 2018-02-05 20:06:34

How to create your own event in JS (you can use JQuery)?

It is required that it be possible to use the construction:

$(elem).on('customEvent', function(param1, param2, param3) { /*...*/ });

customEvent is your created event.
param1, param2, param3 are their own parameters that must be passed when processing the event.
Now I will try to explain, there is Pjax (PushState + Ajax), there is an event: pjax: success, it can be used as:
$(elem).on('pjax:success', function(data, status, xhr, options) { /* ... */ });

And as you can see, their parameters are being passed, I essentially need to do the same. That it would be possible to track the event through the .on () method, as well as that it would be possible to pass various parameters.
---
I looked in the code of that module and did not understand anything. I read on the internet, googled, a lot of information, but I can’t understand anything, perhaps because it’s written difficult for me. Maybe, as usual, only the basics are written, but think about what and how to use further ...
In general, I would be very grateful if you leave not just a link, but write the code here with a short explanation, so that you can later track it through .on () and pass multiple parameters.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Tsvetkov, 2018-02-05
@vityachis

$(document).on('customEvent', function(event, param1, param2, param3) { 
    console.log(param1, param2, param3); 
});
$(document).trigger("customEvent", [ "param1", "param2","param3" ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question