Jump to content

Finding the Index of a list item which contains a link element with a class.


Recommended Posts

Alright so I have this:

 

var tabindex = $("ul#myid").index("li > a.current");

 

The problem is it isn't quite working the way I want it to.

 

Edit: I do see the problem with this, but I'm still not quite sure how to do what I am aiming for.  I know you would do something like $("#myid li").index("#listid");, so I'm still not entirely sure how to get what I want.

 

Take the following code as an example:

 

<ul id="myid">
<li><a href="#blah1">My Link #1</a></li>
<li><a href="#blah1">My Link #1</a></li>
<li><a href="#blah1" class="current">My Link #1</a></li>
<li><a href="#blah1">My Link #1</a></li>
<ul>

 

Basically I need to pull the index of the <li> which contains the <a> that has the current class.  Basically index need to return 2, but either returns 0 or -1.  I am testing it where the current class is on the 3rd to 5th link, so 0 or -1 isn't really an option of what it should spit out.  I figure it is because the <a> is in the <li> which would result in 0 to be the index.  The issue is I cannot place "current" on the <li>, which of course makes things a bit dumb.

 

From what I thought is it would use the <ul> as a primary parent, then select the <li> of the <a> that contains the current class.  Everything in my mind states this is right, or at least should be right.. but obviously it isn't.

 

Any assistance would be greatly appreciated.

 

P.S. I have done some preliminary Googling to find an answer, but I'm kind of on a tight deadline and don't have hours to spend debugging this.  I figure it is a rather simple solution, but so far the results from Google have not landed me in the right place to get the answer I need.

 

Kindest Regards,

Jumpy09

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.