Skip to content

Instantly share code, notes, and snippets.

@petersooley
Last active December 20, 2015 11:59
Show Gist options
  • Select an option

  • Save petersooley/6127673 to your computer and use it in GitHub Desktop.

Select an option

Save petersooley/6127673 to your computer and use it in GitHub Desktop.
This is a jQuery plugin for determining the depth-first position of a child element.
// $('ul').depthPosition('li:nth-child(5)')
(($) ->
$.fn.depthPosition = (targetListItem) ->
count = 1
for child in @children()
if $(child).get(0) == $(targetListItem).get(0)
return count
else
childDepths = $(child).depthPosition(targetListItem)
count += childDepths
return count
)( jQuery )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment