Travis
Travis

Reputation: 10912

Most appropriate way to select first occurrence of a child element in jQuery

I have a list like so:

<ul id="topElement">
  <li></li>
  <li>
    <ul>
      <li>
        <ul>
          <li>
          </li>
        </ul>
      </li>
    </ul>
  <li>

Using jQuery, what is the best aka most efficient way to reference the first occurrence of a ul from #topElement.

That is to say, I do not want this...

$("#topElement ul") 

Which will return every ul element in the tree. I only want the first ul down.

Thanks,

Upvotes: 2

Views: 1877

Answers (4)

Fermin
Fermin

Reputation: 36091

$("#topElement ul").first();

Upvotes: 1

Sean
Sean

Reputation: 29772

$("#topElement > li > ul:first")

Upvotes: 0

Pointy
Pointy

Reputation: 413757

I think that if you want the first "ul" you find inside the contents of the "ul" called "topElement", then you'd want this:

$('ul#topElement ul:first')

Upvotes: 4

Alon Gubkin
Alon Gubkin

Reputation: 57129

$("ul#topElement:first") 

Upvotes: 0

Related Questions