Bug Tracker

Opened 7 years ago

Closed 7 years ago

#13100 closed feature (plugin)

Add a method which combines the results of find and filter

Reported by: david.scourfield@… 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.

Change History (1)

comment:1 Changed 7 years ago by gibson042

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?

Note: See TracTickets for help on using tickets.