3

I'm working on a little web app where clicking on one div (call it element "A") needs to change the position of itself and another div (call it element "B"), and vice versa.

The functionality would go something like this:

Click 'A' {
  A is moved some number of pixels closer to the mouse position (making use of jQuery's .pageX and .pageY event object properties)
  B's click event is manually triggered, moving it some number of pixels closer to the mouse
}

I figured I would just bind click event handlers to both A & B. If A were clicked I would manually trigger B's click event (using jQuery's .trigger(), and vice versa. Unfortunately manually triggering the click event for one object from within the other object's handler isn't working for me--e.g. in the example above, I can't retrieve pageX and pageY properties when triggering div B's click event.

I've included an example to illustrate this point here: http://jsfiddle.net/tZFNk/2/.

Cheers and thanks in advance for your help.

5 Answers 5

6

You mispelled the word "question". Try:

$('#aQuestion').trigger('click');

instead of

$('#aQestion').trigger('click');
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for picking up that typo. I corrected that and switched to '.triggerHandler()' but pageX and pageY are still undefined...
@AlR. that is because you triggered the click programatically
The selector being wrong is not the real issue. @3nigma, you've got it!
2

Trigger the event using the first event's pageX and pageY properties.

$('#aQuestion').bind('click', function(e, f){
    // Use the original pageX property if it exists, else use pageX set by triggered event.
    if (e.pageX !== undefined) {
        alert('x-pos: ' + e.pageX + ', y-pos: ' + e.pageY);
    } else {
        alert('x-pos: ' + f.pageX + ', y-pos: ' + f.pageY);
    }
});

$('#aDiv').bind('click',function(e){
    // Trigger event on #aQuestion with the current click coordinates.
    $('#aQuestion').trigger('click', { pageX: e.pageX, pageY: e.pageY });
    alert('Nothing happens!')
});

http://jsfiddle.net/tZFNk/20/

Comments

2
$('#aQuestion').click();

or

$("#aQuestion").trigger('click');

you have a typo

http://jsfiddle.net/tZFNk/7/

Comments

1

The event properties don't seem to be propagating correctly, as nachito has already identified in his response. However, you don't have to construct and assign values to a new object, just pass the 'e' object in the trigger function:

$('#aQuestion').bind('click',function(e, evt){
    var pageX = e.pageX || evt.pageX;
    var pageY = e.pageY || evt.pageY;
    alert('x-pos: ' + pageX + ', y-pos: ' + pageY);
});

$('#aDiv').bind('click',function(e){
    $('#aQuestion').trigger('click', e);
    alert('Nothing happens!')
});

Comments

0
    $(document).ready(function(){

        $("#button2").each(function(){
            var buttonOld = this;
            $('#button1').bind('click',function(){$(buttonOld).trigger('click')});
        })
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.