ValeriiVasin
ValeriiVasin

Reputation: 8706

Detect browser autoclick

I have some problems in implementing my jQuery plugin. I need a plugin which shows me some element (popup) and hides it when i click out of bounds of this element.

I implemented this plugin. But I have problems with forms in popup. When I calculate coordinates of clicked element I use pageX and pageY properties.

But it is some interesting things when I try to submit form using ENTER key. When I push ENTER browser triggered 'click' event on submit button, but pageX and pageY of this button is equal 0. In event.target I see my button.

When I click submit button myself - everything is OK.

Question: how can I detect who clicked this button? User or browser? (without any spikes like detecting enter key on input or so on)

Additional: as you can see here events are different (when click on submit and press ENTER). But difference is so small... I see only that 'detail' property is set to 0, when we submit form by ENTER. Detail property is amount of clicks on element for mouse event.

Is it only way to detect submission? Is it correct?

Upvotes: 1

Views: 617

Answers (2)

leo.vingi
leo.vingi

Reputation: 1852

If you're binding a click handler to a click event and later calling the click programmatically as well as from a user click and want to differentiate the two events in the handler, you can check for eventObject.originalEvent which is set in the user click but not the .click() call.

Example:

$('#el').click(function(evt){ if(evt.originalEvent){ //user click } else { // .click() call } });

Upvotes: 3

Madara's Ghost
Madara's Ghost

Reputation: 174957

How about testing for pageX == 0 AND pageY == 0?

Or even better, assuming it is a form (it implies from your post), why not use .submit() rather then .click()?

Upvotes: 2

Related Questions