tags:

views:

60

answers:

3

so here's the basic idea. I have a list of links, and an array of href values, I want to filter the links so that my list only contains the links that have an href value that exists in the array. I can do this like so:

var filtered = unfiltered.filter(function() {
    for (var i = 0; i < ids.length; i++)
        if ($(this).is('a[href$=' + ids[i] + ']')) return true;
});

Is this the best way to achieve what I'm looking for?

A: 

I think you want .filter(). No, wait, you gave a misleading title...

I better think some more.

SamB
I changed the title, hopefully it's more succinct.
Master Morality
Less misleading, at least.
SamB
A: 

I usually like doing filtering with classes.

$.each(ids,function(n,i){
  $("a[href=" + i + "]").addClass("filter");
});

This may save you from rerunning the above code if no changes are being made to the filter list.

Drew
Well what happens is I have a list of users and another list that represents users in a role. The list of all users is populated once and every time you select a different role to edit, I just grab an array of user ids that represent users in the role. I then get all the user html elements in the first list that have an id in the array, hide them in the first list and clone them to the second list.
Master Morality
A: 

If your list of IDs is populated once, then just generate the match query once and let jQuery do dynamic matching.

See multiple selector syntax and example.

Then you just go: $(expr, "ul#XYZ") , while changing XYZ to your container for the appropriate bunch of users.

Alexandre Rafalovitch