tags:

views:

1689

answers:

3

jQuery makes it easy to remove nodes from the DOM. But how do you remove something from the jQuery object?

+7  A: 

If you are talking about removing nodes from the jQuery object, use the filter or not functions. See here for more.

How to use filter:

var ps = $('p');

//Removes all elements from the set of matched elements that do 
//not match the specified function.
ps.filter(function() {
  //return true to keep it, false to discard it
  //the logic is up to you.
});

or

var ps = $('p');

//Removes all elements from the set of matched elements that 
//do not match the specified expression(s).
ps.filter('.selector');

How to use not:

var ps = $('p');

//Removes elements matching the specified expression 
//from the set of matched elements.
ps.not('.selector');
geowa4
@geowa4 - thanks for the reply. Do you know if filter removes a node from the object globally, or just local to its function?
Will Peavy
+1  A: 
<ul>
    <li class="1" />
    <li class="2" />
    <li class="3" />
    <li class="4" />
    <li class="5" />
</ul>

Filter iterates over the jQuery object collection. For each of the elements: Return true inside filter() to keep the current item in the jQuery object collection. Return false to remove the current object from the jQuery object collection.

$("li").filter(function ()
{
    if (this.className == "1" || this.className == "2") return true;

    return false;
});

In this case; the anonymous function executed by filter() will return true for the list-item which has the class 1 and/or 2, in turn removing the last three list-items from the jQuery object collection.


A practical example:

<ul>
    <li class="1" />
    <li class="2" />
    <li class="3" />
    <li class="4" />
    <li class="5" />
</ul>

This snippet adds a class ("blue") to the unordered list. Then highlights the first two list-items. Then attaches a click-handler to the first two list-items:

$(function ()
{
    $("ul").addClass("blue").find("li").filter(function ()
    {        
        if (this.className == "1" || this.className == "2") return true;

        return false;

    }).addClass("highlight").click(function ()
    {
        alert("I am highlighted!");
    });
});
roosteronacid
fyi, that'll return false every time. you're looping over `ul`s, not the `li`s which have the classnames.
geowa4
Brain-fart :) Thanks for the heads-up.
roosteronacid
A: 

As noted already, $.filter() is a great option for filtering data. Note also that the jQuery object can be handled like an array, and as such, you can use array methods like splice() on it.

var people = $(".people");
people.splice(2,1); // Remove 1 item starting from index 2
Jonathan Sampson
jQuery is not an Array. Where are the `shift`, `unshift`, `reverse`, etc. functions? I believe it might be an extended NodeList, but I'd have to look into the source to be sure.
geowa4
@geowa4: Read http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper
Jonathan Sampson
@Jonathan: did you read it? bc it doesn't say it's an array at all. you can get one, sure, and jQuery can behave like an array in some ways. but it is still not an array--an important distinction. in fact, few of the array functions work on the jQuery object, as noted in my first comment.
geowa4
@geowa4: Yes, I read it. I know it's not an array (technically), so I apologize for my poor wording. What I meant to communicate was that you can treat it like an array - as my example pointed out.
Jonathan Sampson
@Jonathan: deal. usually i'm not picky about wording, but that is a common mistake that i see a lot of people make that leads them into spending a lot of time scratching their heads.
geowa4