jQuery Get Index of Current Element

By Sam Deering

jQuery code snippet to get index of the current element. Good for those tasks that you use all the time and can simply put into a function for reuse.

$.fn.getIndex = function(){
	  var $p=$(this).parent().children();
    return $p.index(this);
  • Rafi

    Just modified it
    $.fn.getIndex = function(){
    var $sf = $(this), $p = $sf.parent().children();
    return $p.index($sf);


Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.