0

How can I find a visible child element within a parent with jQuery?

Tried e.g. the following (and many others) which does not work.

var childelement = $("#parent").find(".child:visible");

There are many child elements within the parent, but only one is visible in the same time. All child elements are defined with the same class name.

Edit: In my code the child elements are defined to visible or invisible with the display attribute. Similar than below:

<span class="child" style="display: inline;">One</li>
<span class="child" style="display: none;">Two</li>

SOLUTION Got it work with this one:

<div class="child" style="display: inline;">One</li>
<div class="child" style="display: none;">Two</li>
CuriousSuperhero
  • 6,531
  • 4
  • 27
  • 50
  • I added some more code to my example. See above. – CuriousSuperhero Apr 15 '14 at 13:57
  • Actually I used the span element in the beginning. There were problems with that one. Changed it to div and now it works. I didn't copypaste the whole code here because it's quite complex. Now it works. See above. Thanks anyway :) – CuriousSuperhero Apr 15 '14 at 14:18

3 Answers3

1

Here's how you do it :

$('#parent').find(':visible');

jsFiddle

SauriolJf
  • 412
  • 2
  • 10
  • But I would like to find a .child class that is visible. There can be other visible elements within #parent as well. Your code is good, but it finds all classes within #parent that are visible. – CuriousSuperhero Apr 15 '14 at 13:30
  • Ok then when I use your example it still works : http://jsfiddle.net/xqc4v/1/ – SauriolJf Apr 15 '14 at 13:34
  • In my code the child elements are defined to visible or invisible with the display attribute. As seen below:
  • One
  • – CuriousSuperhero Apr 15 '14 at 13:50
  • Ok but it's still working : http://jsfiddle.net/xqc4v/5/ If it works for you please take a two seconds to accept my answer. :) – SauriolJf Apr 15 '14 at 19:05