javascript - Trigger event with parameters -
this pretty annoying. want trigger event in javascript. need pass event object parameters usual , additional custom parameter.
in jquery, super easy:
$('#element').trigger('myevent', 'my_custom_parameter');
but don't want use however. every other question have found relating has suggested 'use jquery!' it's plugin i'm developing , require jquery 1 method pretty silly. can point way above in vanilla js that's cross-browser compatible?
you may create custom events http://jsfiddle.net/9ew6m/
html
<a href="#" id="button">click me</a>
js
var button = document.getelementbyid("button"); button.addeventlistener("custom-event", function(e) { console.log("custom-event", e.detail); }); button.addeventlistener("click", function(e) { var event = new customevent("custom-event", {'detail': { custom_info: 10, custom_property: 20 }}); this.dispatchevent(event); });
output after click on link:
custom-event object {custom_info: 10, custom_property: 20}
more information found here.
Comments
Post a Comment