javascript - Dispatch event with data -
i trying dispatch event pass custom data event listeners listen on event.
considering function fires event:
function click() { var x = "foo"; document.dispatchevent(new customevent("clicked")); } click();
how can pass custom data event listener?
document.addeventlistener("clicked", function(e) { console.log(x); // logs "foo" });
perhaps looking event.detail
new customevent('eventname', {'detail': data})
instead of data use x , in event listener can access x using event.detail
function getselectionbounds() { var x =(bounds["x"].tofixed(2)); var y= "xyz"; var selectionfired = new customevent("selectionfired",{ "detail": {"x":x,"y":y }}); document.dispatchevent(selectionfired); }; document.addeventlistener("selectionfired", function (e) { alert(e.detail.x+" "+e.detail.y); });
Comments
Post a Comment