[ACCEPTED]-Get parent index with jQuery-xhtml

Accepted answer
Score: 43

$(this).parent().index();

short and sweet

Correction : this is not 2 short and sweet

from where this comes it 1 should be like:

$(".bullets li a").click(function(){
    var myIndex = $(this).parent().index();
    console.log(myIndex);
    showPic(myIndex);
});
Score: 28
$(".bullets li a").click(function(){

   var myIndex = $(this).parent().prevAll().length;
   showPic(myIndex);

});

Note: prevAll().length will give you a zero-based index; the 1 first item will be 0 (zero).

Score: 2
$(".bullets li a").click(function(){
    var myIndex = $(this).parent().index();
    console.log(myIndex);
    showPic(myIndex);
});

0

Score: 2

How I'd do it

$(".bullets li a").click(function()
    {
  var $li = $(this).parent();
  var myIndex = $li.parent().children().index( $li );
  alert( myIndex );
});

0

Score: 1
$(".bullets li a").click(function(){
   var me = $(this);
   var myIndex = $.each($("li", $(this).parent().parent()), function(i, item){
      if(item == me){
      showPic(i);
   });
});

0

Score: 1

Here is a solution I created using tables. you 2 can see how I used the selectors. Im sure 1 it could be cleaner, but here you are:

    $('.showColumn').live('click',function() {
        var theEl = {{id of the element}};
        var setEl = $('th#' + theEl + '');
        var index = setEl.index(setEl.parentNode);
        $('table tr').each(function() { 
            $('th:eq(' + index + ')',this).show(); 
            $('td:eq(' + index + ')',this).show();
        });
    });

cheers.bo

More Related questions