From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Matt
Subject: [jQuery] How to tell numerically which sibling element
I'vetriggered?
Hello, all jQuery gurus. I deeply appreciate all the help people provide
one
another on this list, and how generously the experts contribute their
time
and wisdom to newcomers such as myself. I finally have an issue I'd like
some help with myself, and any assistance would be welcome.
I have an unordered list like this:
<div id="articlesIndex">
<ul>
<li><a href="article01.php>First Article</a></li>
<li><a href="article02.php>Second Article</a></li>
<li><a href="article03.php>Third Article</a></li>
</ul>
</div>
I cannot figure out how (or whether) I can use jQuery to read "which LI
element has just been clicked" as a numeric index. Here is a simplified
example code:
$(document).ready( function() {
$('#articlesIndex li a').click( function() {
alert("You just clicked list item number: ");
return false; // prevents the hyperlink from firing
} );
} );
Now how do I pass the number so that the alert "You just clicked list
item
number: " is followed by the number just clicked on? When you click on
the
hyperlink on the second list item, I want to read that index so jQuery
can
"know" that I've just clicked sibling number 1 out of the possible
siblings
0,1, and 2. So it would be something like:
alert( "You just clicked list item number: " + $this.myNumericIndex() );
Which is obviously complete gibberish since no such thing exists, but
you
get the idea--I want to know what DOES exist that I can use there. I
can't
make heads or tails of the "index()" method and don't know whether it's
relevant here. (I know I could just assign id's to each of them and
simply
reference their id attribute, but that's a pain in the butt and requires
extra inelegant code.)
Any suggestions?
-----
You're close, you want to look at .index()[1] and use that. Here is some
untested code that should point you in the right direction.
$(document).ready( function() {
$('#articlesIndex li a').click( function() {
currIndex = $("#articlesIndex ul
li").index($(this).parent(li)[0]);
alert("You just clicked list item number: " + currIndex);
return false; // prevents the hyperlink from firing
});
});
Basically we are grabbing all the LI elements within #articleIndex and
then finding the index of the LI that contains the A element the user
clicked on in the first place.
[1] - http://jquery.bassistance.de/api-browser/#indexElement
-ALEX
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/