1

What i have:

$('#main').find('*').on('click.stop', function() {return false;});

What i want: Changing the selector '*' to all classes with a bound click_handler because of performance issues.


My idea: Assigning additional classes when binding a handler to a class.


Problem with idea: The amount of code i would have to change is too damn high! and i am very lazy...

Miiller
  • 1,063
  • 1
  • 10
  • 29

3 Answers3

3

You can select all elements with a clickhandler like that:

var e = $.data($(body).get(0), 'events').click;

If you have done that, you can loop through these elements with the $.each loop and apply your function/ event to them :)

Stefan Fandler
  • 1,141
  • 7
  • 13
3

You can write your expression to do this. jQuery does not support this selector natively.

jQuery.extend(jQuery.expr[':'], {     
  click: function(elem) { 
       return $(elem).hasEvent('click');    
  } 
});

Then, you can use: $("*:click").on('click.stop',...

hasEvent:

$.fn.hasEvent = function(event, fn) {
if (!event) { return this; }
var has = {
      event: false,
      namespace: false,
      handler: false
    },
    events = this.data("events"),
    namespace = event.split(".");
event = namespace.shift();
namespace = namespace.join(".");
if (events) {
  if (!namespace) { has.namespace = true; }
  if (!fn) { has.handler = true; }
  if (event in events) {
    $.each(events[event], function(i, v) {
      if (namespace) {
        if (namespace === v.namespace && event === v.type) {
          has.namespace = true;
          has.event = true;
          if (fn && fn === v.handler) { has.handler = true; }
        }
      }
      else {
        if (event === v.type) {
          has.event = true;
          if (fn && fn === v.handler) { has.handler = true; }
        }
      }
    });
  }
}
return has.event && has.namespace && has.handler;
};
Pethical
  • 1,472
  • 11
  • 18
0

jQuery check if event exists on element

the answers to this may help you. this question was to check if an event exists on an element.

Community
  • 1
  • 1
Dave Haigh
  • 4,369
  • 5
  • 34
  • 56