Kieran
Kieran

Reputation: 656

Filtering through a list of JQuery selected items

I am in a situation where I haven't found a selector or a selector function that quite does what I would like it to do.

Therefore I am trying to filter the list to contain only the items I would like it to.

I have a selector

var html = $(".foo .foobar")

This returns what I wanted it to.

Then I have a for loop that loops through those selected items and identifies the ones I want to keep in that list.

However, I need to keep the modified list the same type as a selector so that I can perform jquery actions to them later.

how do I create a copy of the 'html' variable (or a filtered original) but with only the desired rows that were found in the function (Keeping it still in a state as if it was a selector itself)?

Later I have an 'each' loop that begins like this:

html.each(function(i, el) {
   $(this).replaceWith(tempArr[i]);

I am trying to achieve a result where 'html.each' has 'html' as the modified list previously selected.

Thanks.

// Update

var htmlTemp;

for (var primaryCounter = 0, secondryCounter = 0; primaryCounter < htmlTemp.length; primaryCounter++) {

  if (firstFound) {
    secondryCounter++;
    if (secondryCounter % columnCount === 0) {
      html.push(htmlTemp[primaryCounter]);
    }
  } else {
     if (primaryCounter === currI) {
        html.push(htmlTemp[primaryCounter]);
        firstFound = true;
     }
  }


}

Above is the function including the logic that I wanted to use (Which isn't going to run). Is there a way with 'filter' possibly where I can call this function and instead of 'push()' just include at these indexes found? Thanks.

Upvotes: 0

Views: 104

Answers (1)

Pratyush Sharma
Pratyush Sharma

Reputation: 257

Assuming html as an array, you can use html.filter(callbackFunc) to get a new list every time.

Check this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Upvotes: 1

Related Questions