This may seem strange, but I'm working on a plugin that needs to find elements in the div or the div itself.
The script finds the element based on user selection, but the content, including the markup, is a variable. Thus, the script will search for the element as follows:
$('.block').find(selector);
but there is no easy way to select the ".block" selector. Choosing a parent before using find is not a solution, as there are several .block elements.
I know that the selector extension expr[":"] will not work, as it only searches for children. But, I figured out the way to โ duck punch โ this method by making the selector โ: selfโ:
(function($){ var orig = $.fn.find; $.fn.find = function(sel){ return (sel === ':self') ? this : orig.call(this,sel); } })(jQuery)
But this is a bit like the top. And this will slow down jQuery's processing with a tiny bit with every search function. Is there any other way to do this?
Thanks for answers! But I ended up with this:
var b = $('.block'), el = (b.is(selector)) ? b : b.find(selector);
jquery jquery-selectors
Mottie
source share