9

Possible Duplicate:
Getting the ID of the element that fired an event using jQuery

How do we find out what triggered an event?

I want to find out if a search input was triggered using the enter key or by clicking on a button.

Community
  • 1
  • 1
Hussein
  • 42,480
  • 25
  • 113
  • 143
  • 1
    see this [answer](http://stackoverflow.com/questions/48239/getting-the-id-of-the-element-that-fired-an-event-using-jquery/48684#48684). – Adeel Feb 08 '11 at 06:43
  • Not a duplicate. For the **DOM object** that triggered the event see the putative duplicate question (answer is `event.target.id`). For the **USER EVENT** that triggered the event, which is what the OP is asking, see the @a'r comment below (answer is `event.type`). – Bob Stein Jun 27 '13 at 18:45

1 Answers1

6

I assume your binding to the search input using jQuery handlers as such. So just pass the event type along. For more information, pass the entire event object along:

$("input.Search").click(function(event) {
    doMySearch(this, "click");
}).keyup(function(event) {
    doMySearch(this, "keyup");
}); 

function doMySearch(element, eventtype) {
   ...
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raynos
  • 166,823
  • 56
  • 351
  • 396