I need to find an elements current position within the DOM (preferably when clicked).
With jQuery, I get it if I know the position beforehand:
$('li').get(3);
But there are times when I rely on $(this)
to get the current position:
HTML:
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
JS:
$('li').click(function(){
// this does not work
console.log($(this).get());
// this does, but I won't know which is clicked
console.log($('li').get(3));
});
What's the best way for me to get the current position? Preferably as a number, so I can use it elsewhere.