9

Possible Duplicate:
How all events of dom element can be bind?

I have an object that gets events triggered against it.

How do I bind all the events and console.log the event name?

Community
  • 1
  • 1
Harry
  • 52,711
  • 71
  • 177
  • 261

1 Answers1

8

You can bind all the standard javascript events by including them in the bind call separated by spaces. The jQuery docs provide this list:

blur focus focusin focusout load resize scroll unload click dblclick mousedown mouseup  mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup error

So, to bind the event for click, blur and focus:

   $('#foo').bind('click blur focus', function(event) {
      console.log(event.type);
    });

If you're looking for custom events, you'll have to look at the API and bind those as well. There doesn't appear to be any way to bind on any event.

Mazatec
  • 11,481
  • 23
  • 72
  • 108
Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
  • 2
    I don't think this directly answers the question. Reading between the lines, I can sort of see where you're going, but there has to be a better way. – Wylie Jul 05 '11 at 03:43
  • @Wylie The jQuery API doesn't provide a way of doing it, and the native javascript way of binding events is with onevent="code" attributes, or through AddEventListener. I suppose there *could* be a plugin out there somewhere. – Kevin Stricker Jul 05 '11 at 03:47
  • 2
    Hmm, turns out this question is an exact duplicate: http://stackoverflow.com/questions/5848598/how-all-events-of-dom-element-can-be-bind All the accepted answer there is doing is auto-generating the list of javascript events. It still wouldn't handle custom events. – Kevin Stricker Jul 05 '11 at 03:54