active class is on the <a> tag not <li> so example doesn't work

try this:

               var arr = [ ];
            $(".active").each(function() {
              indexLi=$(this).parents(".buttons").children("li").index($(this).parent("li"));                   
                arr.push(indexLi);             
            });
            alert(arr);


Dave Ward wrote:
I don't think there's a shortcut in jQuery to do exactly that.  You
could iterate over them and build the array like this though:

var arr = [ ];

$(".buttons li").each(function(i, item) {
  if ($(item).hasClass("active")) {
    arr.push(i);
  }
});

On Jul 11, 1:33 pm, yo2lux <yo2...@gmail.com> wrote:
  
I have a HTML list:

<ul class="buttons">
   <li><a href="" class="active">Home</a></li>
   <li><a href="">About</a></li>
   <li><a href="" class="active">News</a></li>
</ul>

I want to obtain the index only for the elements with class="active".
For example I want to create an array with value "0" for Home and "2"
for News (the indexes).

Is possible to do this with jquery?
Thanks!
    

  

Reply via email to