javascript - How to get index of clicked link in jQuery? -


this markup:

<div> <h2><a href="link-here">link text</a></h2> <p>description</p> <h2><a href="link-here">link text</a></h2> <p>description</p> <h2><a href="link-here">link text</a></h2> <p>description</p> .. unknown number of such elements... </div> 

when user clicks link wanted index. tried use $(this).index(); returns zero. how can link?

the reason issue index() return result relative parent of this, h2 element. in other words, index() 0 case.

however, if read documentation, you'll find index() has variants, such 2nd option can specify how retrieve index list of items via selector.

here's example: https://jsfiddle.net/d7rpuv6d/ (please open console see result)


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -