jQuery - selecting elements from inside a element

Alex picture Alex · Apr 27, 2011 · Viewed 228.4k times · Source

let's say I have a markup like this:

<div id="foo">
  ...
  <span id="moo">
    ...
  </span>
  ...
</div>

and I want to select #moo.

why $('#foo').find('span') works, but $('span', $('#foo')); doesn't ?

Answer

Jishnu A P picture Jishnu A P · Apr 27, 2011

You can use any one these [starting from the fastest]

$("#moo") > $("#foo #moo") > $("div#foo span#moo") > $("#foo span") > $("#foo > #moo")

Take a look