Dilip Rajkumar
Dilip Rajkumar

Reputation: 7074

Why does jQuery considers 'Enter' Keypress event as Click event

Consider a following code:

$(document.body).click(function(e) {
console.log(e.clientX);
});

This event is supposed to fire when ever I make a click. But I have a search bar in my application. When ever I type something in the search bar and press enter it is calling the click event. Is that a bug in jQuery or I am missing any JS concept.

To replicate this issue, go to www.ebay.in then open firebug in FF then run the above script in console.

  1. Then click anywhere you should get the X coordinate.
  2. Place the cursor in the search bar then press enter having 'Persist' on on the console. You will see 0

Any clarification would be really helpful.

Upvotes: 0

Views: 81

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Pressing Enter inside an input field simulates a click on the first submittable button on the form. This is standard and desired behaviour, but when blindly accepting click events it may produce an otherwise unexpected click.

Upvotes: 5

Related Questions