Nick
Nick

Reputation: 19684

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

I want to know if it is possible to add properties to the event object that gets passed to the bind() event handler function. I have a piece of code that this bound to "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 trigger the event; namely 'pageX' and 'pageY'.

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

I'd like to trigger the handler below and 'fake' the pageX. Can that 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);                    
            });

Upvotes: 4

Views: 1591

Answers (2)

Steve Wellens
Steve Wellens

Reputation: 20638

Here's an 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);
    }

Upvotes: 0

krtek
krtek

Reputation: 26617

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

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

Upvotes: 3

Related Questions