Is it possible to dynamically modify an event object? jQuery trigger () / bind ()

I want to know whether it is possible to add properties to an event object that is passed to the bind () event handler function. I have a piece of code that is associated with "mousedown". I would like to trigger this event explicitly. The problem is that the bind () handler expects some event properties that are provided when the mouse fires the event; namely "pageX" and "pageY".

I know that I can pass additional parameters as an array to Trigger (), but I would prefer not to change the bind () code. This may not be possible.

I would like to call the handler below and fake pageX. Can this be done somehow?

currentObj.bind("mousedown", function(e) { var handleElement = currentObj.find(".HwSliderHandle"); var offsetMultiplier = calculateOffsetMultiplier(); var newPosition = Math.round(e.pageX/offsetMultiplier) - sliderInfo.contextOffset.left; moveHandleTo(handleElement, newPosition); }); 
+4
source share
2 answers

You can create your own event object and then send it using trigger () :

 var e = new jQuery.Event("mousedown"); e.pageX = 'fake value'; currentObj.trigger(e); 
+3
source

Here's the approach:

  $(document).ready(DocReady); function DocReady() { $("#test").bind("mousedown", mouseDown); } function mouseDown(e) { alert("X: " + e.pageX + ", Y: " + e.pageY); } function fakeIt() { var e = { pageX: 40, pageY: 50 }; mouseDown(e); } 
0
source

Source: https://habr.com/ru/post/1343676/


All Articles