On 12/03/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?

each has a parameter which is the index:

$(document).ready( function() {
 $('#articlesIndex li a').click( function(i) {
    alert("You just clicked list item number: " + (i + 1));
    return false; // prevents the hyperlink from firing
 } );
} );

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to