Try this:
$(document).ready( function() {
var $listItems = $("#articlesIndex li");
$listItems.find('a').click( function() {
var itemNum = $listItems.index($(this).parent()[0]) + 1;
alert("You just clicked list item number: " + itemNum);
return false; // prevents the hyperlink from firing
} );
} );
On 3/12/07, Matt <[EMAIL PROTECTED]> wrote:
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?
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/
--
Aaron Heimlich
Web Developer
[EMAIL PROTECTED]
http://aheimlich.freepgs.com
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/