views:

62

answers:

4

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,

A: 
$("ul#topElement:first") 
TTT
I don't think that's right - he wants the first "ul" underneath the top "ul".
Pointy
+2  A: 

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')
Pointy
why ul#topElement? id is unique. #topElement should be faster.
David
You're right of course, but sometimes I do that just to help myself not be confused. It's not necessary, and in fact it can come back and bite you if you change your markup a lot. In this case I think I was just trying to make clear how the solution related to the problem posed. I doubt there's any measurable performance difference, however, as sizzle knows exactly what you know about the semantics of an "id" value :-)
Pointy
It does what it says on the box. :)
Travis
A: 

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

Sean
+1  A: 

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

Fermin