JSFiddle - React, Tailwind, and code Playground

by Hugo Carneiro

HTML

<div>
  <div>

  </div>
  <div>

  </div>
  <div>

  </div>
</div>
<div>
  <div>

  </div>
  <div>

  </div>
  <div>
    <div>

    </div>
    <div>
      <p>
        hello
      </p>
      <p class="marker">
        world
      </p>
    </div>
    <div>

    </div>
  </div>
</div>
<div>

</div>

JavaScript

function collectionArray($element) {
	var array = []
  
  function recursiveCollection($el) {
  	if ($el.is('body')) {
      return array
    }

    var index = $el.index()
    array.push(index)
    var parentIndex = recursiveCollection($el.parent())

    return array
  }
	
  return recursiveCollection($element)
}


var $marker = $('body').find('.marker')
var collection = collectionArray($marker).reverse()
console.log(collection)

var arrayLength = collection.length
var count = 0

collection.forEach((index, idx) => {
	count++
  
  if (count === arrayLength) {
  	console.log(index)
    console.log(idx)
  }
})