Page 1 of 1

JS code

Posted: Wed Nov 19, 2014 2:43 am
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 ??

Re: JS code

Posted: Wed Nov 19, 2014 6:55 am
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).