0

What does the + 1 means in this example? In this statement: if ($('.hideme').index($(this)) + 1 === $('.hideme').length)

http://jsfiddle.net/e5qaD/1151/

It's from the answer of one of the questions here, on stackoverflow: Show Div when scroll position

Community
  • 1
  • 1
mikefromnl
  • 13
  • 1

2 Answers2

2

index starts from Zero, and the length Property for a non-empty element starts from 1.

Jquery index method Searches for a given element from among the matched elements.

From Jquery Documentation

If a selector string is passed as an argument, .index() returns an integer indicating the position of the first element within the jQuery object relative to the elements matched by the selector. If the element is not found, .index() will return -1.

In most Programming languages, the index starts from 0. So, the index returned would be always 1 less than the actual number.

Abhinav Galodha
  • 7,820
  • 2
  • 27
  • 37
0

It's to do with how we number elements in programming. When counting in arrays we start from 0

<div></div> 0
<div></div> 1
<div></div> 2
<div></div> 3
<div></div> 4

However, if you want to know how many there are there are 5. So you can add 1 to the answer to get the correct result count

Blank
  • 60
  • 1
  • 5