5

I have a parent with li's, and given the pointer to a li, I want to get it's position as a child.

For this I used:

li.index()

Now... I have one more condition, it should find an index only among these children that have display:block property in css.

I tried it some other ways around but I was unable to solve it. Do have any ideas?

Edit: PS: or, rather for these that do not have display:none property.

EDIT 2: Well I these all require the reference to parent or specific ids, but what if have only a pointer to a li, for example:

<ul>
<li>Foo</li>
<li>Bar</li>
<li>Fiz</li>
<li>Buz</li>
</ul>

li=$('li:nth-child(n)');

now, let's suppose I know only one variable, and I want it's index among these that don't have css display: none propery...

Solved This is what did it:

li.add(li.siblings()).filter(':visible').index( li );

Thanks for helping me out with great ideas and different approach. :)

5
  • 2
    Have you tried li.filter(":visible").index() ? Commented May 14, 2012 at 6:10
  • not yet, give me a sec I will try it out. Commented May 14, 2012 at 6:12
  • No... - li.filter(":visible").index() never works. Commented May 14, 2012 at 6:13
  • Isn't this what you want: jsfiddle.net/zerkms/8nKCw ? Commented May 14, 2012 at 6:16
  • possible duplicate of jQuery indexOf function? Commented May 14, 2012 at 6:52

3 Answers 3

5

With the following markup:

<ul>
  <li>Foo</li>
  <li>Bar</li>
  <li style="display:none">Fiz</li>
  <li>Buz</li>
</ul>

We can determine the (zero-based) index of the last li all visible list items:

// 2
$("li:last").index("li:visible");

If we were to remove the :visible selector, and ask for the index of the last among all:

// 3
$("li:last").index("li");

Update: Using a Variable

If you had a variable reference to a specific list item:

var lastItem = $("#parent li:last");

You could still get its index among visible children in the same container:

var index = $(lastItem).index("#parent li:visible");

Update 2: No Explicit Parent ID

var index = lastItem.parent().find("li").index( lastItem );
Sign up to request clarification or add additional context in comments.

4 Comments

yeah, but I mean this requires both parent id, and list id with: last attached, but isn't there any way I can use something like this: var li= /*$ my li; */ li.index(li.parent())... or something like that...
@user1125062 See "Update 2" in my answer above.
Thanks, I tried all of them for a dozen of time. But now I seem to finally get the point of how it works. I didn't think of it to select the sibling before filtering, but now it works, and this is how I've put it :) li.add(li.siblings()).filter(':visible').index( li )
THanks for helping with different approaches, it has helped me to understand how it works =)
0

TRY THIS ANS

   var listItem = document.getElementById('bar');
   alert('Index: ' + $('li').index(listItem)); 

fiddle link http://jsfiddle.net/VqeHW/

Comments

0

Give your display:none elements a class, then use the .class selector:

$('#theList .visible').each(function () {
  var index = $('#theList .visible').index(this);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.