Is is possible to pass an object instead of a selector as the first argument for jQuery delegate?
var ancestor = $('ancestor'),
children = ancestor.find('a');
ancestor.delegate(children, eventType, handler);
Instead of the regular:
ancestor.delegate('a', eventType, handler);
EDIT
Motivation:
var children = $('a[href^="#"]').map($.proxy(function(i, current) {
var href = $(current).attr('href');
if(href.length > 1 && givenElement.find(href).length === 1) return $(current);
},
this));
$(document).delegate(children, eventType, handler);
I want to delegate only the anchor elements that are hash linked to any element as a child of a given element. Basically I want to do something you can’t do with just a selector only.
You could always just set up the delegation and then do your predicate inside the event handler:
If you wanted to avoid the runtime price of doing that jQuery DOM search inside the handler, you could pre-tag all the “good” tags:
Then your delegated event handler can just check
which will perform well enough to not be a problem under any circumstances.
The reason you have to start with a selector for “.delegate()” to work is that that’s the way it’s implemented. The event handler always does something like:
Now, clearly it could also try and compare the actual elements in the case that you set up a delegate without a selector, but it just doesn’t.
edit — @DADU (the OP) correctly points out that if you go to the trouble to mark everything with a class name, then you don’t even need a fancy event handler that tests; an ordinary “.delegate()” will do it. 🙂