Skip to main content

Bug Tracker

Side navigation

#13100 closed feature (plugin)

Opened December 20, 2012 02:57PM UTC

Closed December 21, 2012 11:29PM UTC

Add a method which combines the results of find and filter

Reported by: david.scourfield@llynfi.co.uk Owned by:
Priority: undecided Milestone: None
Component: traversing Version: 1.8.2
Keywords: Cc:
Blocked by: Blocking:
Description

find() matches elements which are descendants of those in the jQuery object and filter() reduces the set of elements in the jQuery object to those which match the selector. What I would like to use is a function which combines both of these to return the same as:

jQueryObject.filter(selector).add(jQueryObject.find(selector))

Use Case

Currently, I have a method which I run to initialize my content on DOM ready and on receiving/creating new content. For example:

var myInitFunction = function($content){
    // $content is a jQuery object.  Do some init on content here, eg:
    $content.filter('div.clickable').add($content.find('div.clickable')).css('cursor', 'pointer').click(function(){
        // handle click
    });
};

// content needs to be initialized on load, so:
$(document).ready(function(){
    myInitFunction($('body'));
});

/* ...
 *
 * Occasionally, this page receives content via AJAX which 
 * also needs to be initialized, e.g.:
 */

$.get({
    success: function(data) {
       // add data to the DOM here
       // then the new content needs to be initialized, too, so:
       myInitFunction($(data));
    }
});

As the DOM nodes which need to be initialized could either be directly in the jQuery object, or descendants of the nodes therein, $content.filter(selector).add($content.find(selector)) needs to be used to get them all.

Attachments (0)
Change History (1)

Changed December 21, 2012 11:29PM UTC by gibson042 comment:1

component: unfiledtraversing
resolution: → plugin
status: newclosed

We're generally not inclined to enlarge the core API for narrow/redundant cases like this. Does $collection.find( selector ).addBack( selector ) work for you?