- You don’t need inline event handlers
- You can use
event delegation
- Use
index
to get the clicked element index inul
HTML
<div class="grid_12">
<ul id="categories">
<li class="filter">Categories:</li>
<li id="ny"><a href="#newYork"> New York</a>
</li>
<li id="sc"><a href="#spanishCities">Spanish Cities</a>
</li>
<li id="gv"><a href="#aGlasgowViewpoint">A Glasgow Viewpoint</a>
</li>
<li id="sch"><a href="#someChurches">Some Churches</a>
</li>
<li id="bh"><a href="#barcelonaHighlights">Barcelona Highlights</a>
</li>
<li id="mp"><a href="#martin's Pictures">Martin’s Pictures</a>
</li>
</ul>
</div>
Javascript
var id;
$('#categories').on('click', 'li>a', function () {
id = $(this).closest('li').index();
});
2
solved Javascript: Issue when using .click method