Jatin
Jatin

Reputation: 14269

value of `this` inside .filter() method of jquery

here is the HTML I was working with

<ul>
      <li><strong>list</strong> item 1 -
        one strong tag</li>
      <li><strong>list</strong> item <strong>2</strong> -
        two <span>strong tags</span></li>
      <li>list item 3</li>
      <li>list item 4</li>
      <li>list item 5</li>
      <li>list item 6</li>
</ul>

and I was modifying it using .filter() method in two ways:

First:

$('li').filter( function(foo) {
    console.log(this);
    return foo == 2;
}).css('background-color', 'red');

Second:

$('li').filter( function(foo) {
    console.log(this);
    return foo % 3 == 2;
}).css('background-color', 'red');

In first case, Chrome logged this: enter image description here and in second case this: enter image description here

Why does the console display different values of this in different cases?

Upvotes: 0

Views: 79

Answers (1)

user1106925
user1106925

Reputation:

"Why does the console display different values of this in different cases?"

It doesn't. Assuming the DOM hasn't changed, it is displaying the same elements both times. The actual values are no different.

The only difference is how they are visually represented. This has no impact on what they actually are.

Upvotes: 1

Related Questions