I am using $('.gallery img').click
to call a function and using .length
to get a value to us in a comparison, I need to get the position of img in the div. so the .gallery div is like an array and the img in it is a number "0 to 9" so I click the third img and get 2 or the fifth one and get 4.
Asked
Active
Viewed 947 times
-2

Drew
- 1,171
- 4
- 21
- 36
-
3Can you provide some sample HTML, and the function you mention. – Apr 14 '14 at 23:33
-
Sounds like a very similar question to [this](http://stackoverflow.com/questions/4654028/how-to-get-the-element-number-index-between-siblings) – dsaa Apr 14 '14 at 23:34
1 Answers
0
You can use the jQuery index
method for getting the index of the clicked image element in a jQuery collection:
var $img = $('.gallery img').on('click', function() {
var i = $img.index(this);
});
If the image wrapper has only image children you can can also use the index
method without passing the element:
var i = $(this).index();

Ram
- 143,282
- 16
- 168
- 197
-
This worked, the way I implemented it was like so "gal_id = $('.gallery img').index(this);" – Drew Apr 14 '14 at 23:40