I’d like to make a new event type in JavaScript. Say I have some criteria for when a user hasAquiredIceCream, and I’d like to trigger and bind to this event based on the criteria. How could I make a new event in JavaScript? I’m using jQuery, so ideally I’d be able to use it with .trigger('hasAquiredIceCream') and .bind('hasAquiredIceCream'). I’m assuming a pure JavaScript solution would work with jQuery just fine (it wouldn’t need to be a plugin), can this be done?
I’d like to make a new event type in JavaScript. Say I have some
Share
Using jQuery’s
.trigger(), you can trigger custom events on any element or on a JavaScript object.Or, using plain JavaScript, implement the observer pattern: