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 ?
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 ?
Why not just use:
or
$('span', $('#foo'));
works fine on my machine ;)This method is called as providing selector context.
In this you provide a second argument to the jQuery selector. It can be any css object string just like you would pass for direct selecting or a jQuery element.
eg.
The above line will select all spans within the container having the class named
cont1
.DEMO
both seem to be working.
see fiddle: http://jsfiddle.net/maniator/PSxkS/
You can use
find
option to select an element inside another. For example, to find an element with id txtName in a particular div, you can use likeYou can use any one these [starting from the fastest]
Take a look
Actually, $('#id', this); would select #id at any descendant level, not just the immediate child. Try this instead:
or
or