Wyatt
Wyatt

Reputation: 495

If selector has attr of array of attrs

Here is my array of attributes :

var newIDs = [];
    $element.parents(".comments").find(".comment.new").each(function(idx, el){
    newIDs.push($(this).attr('commentID'));
});

Now I have a selector :

 html = $(html).find('.comment')

I want to narrow down the selector to test if the .comment has any of the arributes in the array above.

Upvotes: 0

Views: 41

Answers (2)

guest271314
guest271314

Reputation: 1

You can use .filter(), iterate .attributes of .comment elements

 html = $(html).find('.comment').filter(function(i, el) {
   for (var i = 0; i < el.attributes; i++) {
     if (newIDS.indexOf(el.attributes[i].value) !== -1) {
       return true
     }
   }
   return false
 })

Upvotes: 0

shadymoses
shadymoses

Reputation: 3443

You can use filter:

html = $(html).find('.comment').filter(function() {
  return newIDs.indexOf($(this).attr('commentID')) > -1
})

Docs here

The .filter method filters your jQuery collection to a set of items based on an evaluation function. If your function returns true, the item stays, otherwise it's filtered out.

Upvotes: 1

Related Questions