/    Sign up×
Community /Pin to ProfileBookmark

jQuery(window).trigger() to vanilla JavaScript

Hi there,

In a nutshell, my question is quite simple:

>

How to translate the triggering of a custom event with some bespoke data from jQuery to vanilla JavaScript?

Here is a small function, that used to work immaculately with jQuery:

“`
function _trigger(t, e){ //trigger a custom event
e = e ? e : RQ(“e”); //if event data not passed, then use existing internal event data
jQuery(window).trigger(“pronto.” + t, e); //always prepend “pronto.” and pass the bespoke data in the second variable
}
“`

(I have researched this on the web a lot, but the results were a bit confusing. There seem to be several approaches)

Thanks in advance!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@codyhillJul 12.2021 — This is what you mean?
``<i>
</i>window.addEventListener('trigger', () =&gt; {

})<i>
</i>
``
Copy linkTweet thisAlerts:
@arvgtaauthorJul 12.2021 — Thanks for your reply - much appreciated!

No, I don't mean adding a listener but actually triggering a custom event with some bespoke data something like this:

``<i>
</i>_trigger = (t, e) =&gt; {
window.dispatchEvent(
new CustomEvent("pronto." + t, { detail: { e ? e : RQ("e") } })
)
}<i>
</i>
`</CODE>

At the moment, I'm using the following code, which seems a bit dodgy to me:

<CODE>
`<i>
</i>_trigger = (t, e) =&gt; {
let ev = document.createEvent('HTMLEvents');
ev.initEvent("pronto." + t, true, false);
ev.data = e ? e : RQ("e");
window.dispatchEvent(ev);
document.dispatchEvent(ev);
}<i>
</i>
``
Copy linkTweet thisAlerts:
@mahbubJul 14.2021 — This very good tips. I like it dear.
×

Success!

Help @arvgta spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...