views:

58

answers:

3

while selecting the desired element is simple in jquery i haven't found one way to receive an elements index here's an example of what i want.

$('#minia .holder a').click(function () {

})

this function is used obviously to catch any clicks on one of thoose elements to catch that specific element I use $(this) now my question is how would i get $(this) index so i can then later use it like so $('#minia .holder a[index]') hope you got my point

A: 

try running this and get the idea:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

  <script>
  $(document).ready(function(){

    $("div").click(function () {
      // this is the dom element clicked
      var index = $("div").index(this);
      $("span").text("That was div index #" + index);
    });

  });
  </script>
  <style>
  div { background:yellow; margin:5px; }
  span { color:red; }
  </style>
</head>
<body>
  <span>Click a div!</span>
  <div>First div</div>
  <div>Second div</div>
  <div>Third div</div>
</body>
</html>

a demo and documentation can be found here.

edit

$('#pic .control').click(function () { 
     var imgUrl = $('#view').attr("src"); 
     // why not erase the next two lines... then
     //var indexLink = $('#minia .holder a').index('#minia .holder a[href="'+imgUrl+'"]'); 
     //$('#minia .holder a:eq('+indexLink+')').click(); 
     // use this 
     $('#minia .holder a[href="'+imgUrl+'"]').trigger('click'); // if you wanted a click..
}) 
Reigel
if this works your the best
Breezer
let us see.. :))
Reigel
darn i just tried it but i got 0 index each time $('#pic .control').click(function () { var imgUrl = $('#view').attr("src"); var indexLink = $('#minia .holder a').index('#minia .holder a[href="'+imgUrl+'"]'); $('#minia .holder a:eq('+indexLink+')').click(); })this is the code
Breezer
@Reigel: What is the point of pasting all the sample code from jQuery? Plus you should indicate you took the code from jQuery doc.
Jay Zeng
well idd this code would have been good if it hadnt been that i actually wanted to click the element comming after it
Breezer
if you want the next one, you can use `.next()` like `$('#minia .holder a[href="'+imgUrl+'"]').next().trigger('click');` cheers! :))
Reigel
well, you still have to choose an answer... :))
Reigel
but will that work? considering the selector will only select elements having the link with that specifik image?
Breezer
A: 

try this

$('#minia .holder a').click(function (event) {

var temp_index = $(this).index();

})
chirag
var temp_index = $(this).index(this); // on jQuery 1.4.x
TiuTalk
A: 

well i solved it by doing like this

 $('#pic .control').click(function () { 
         var imgUrl = $('#view').attr("src");
         var indexLink = $('#minia .holder a').index(jQuery('#minia .holder a[href="'+imgUrl+'"]')) + 1;
         $('#minia .holder a:eq('+indexLink+')').click();
      })

even thought you guys answered my initial question this was the final one

Breezer
lol... okay.. good!!!.. and ever you wanted, I have updated my answer... :)) cheers!
Reigel