paul seems
paul seems

Reputation: 565

On click - find the index of the clicked element - how many times it appeared before?

$(document).on('click', '.productPhoto', function(){
  console.log($(this).prevAll(".productPhoto").length); // * this is photo number 3
});

I need to check how many productPhoto elements there are before the clicked one (= its index)

This is not working because prevAll is for siblings, and i need to search how many times productPhoto appears under some container(not sibling) before the clicked one.


So - in the container named products, what is the index of productPhoto that clicked ?

Upvotes: 0

Views: 23

Answers (1)

paul seems
paul seems

Reputation: 565

Well, its as simple as console.log( $( ".productPhoto" ).index( this ) );

Upvotes: 1

Related Questions