JS code

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

JS code

Post by gautamz07 »

Hey guys check out this simple code snippet :

The HTML :

Code: Select all

<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>

Code: Select all

$( "li" )
  .filter(function( [color=#BF0000][b]index[/b][/color] ) {
    return $( "strong", this ).length === 1;
  })
    .css( "background-color", "red" );
now what does the "index" word hold or represent ? why is it needed ??
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: JS code

Post by Celauran »

It's the index of the array you're iterating over and allows use of "this" inside the callback. So if you consider

Code: Select all

<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>
then item 3 has an index of 2 (the first indexed value is 0).
Post Reply