JavaScript: Getting a data attribute from an onclick event

From The Travis van der Font Wiki
Jump to: navigation, search
Using jQuery
$("#sticky_posts").on('click', function (event)
{
   alert($(event.target).data('index'));
});
HTML Example
<ul id="custom_list">
  <li data-index="1" class="active"><a href=""></a></li>
  <li data-index="2"><a href=""></a></li>
</ul>

Reference(s)