There are several ways to do this. If you have a document like this:
<html>
<body>
<p>Test</p>
<div id="box">
<p>Hi There</p>
<div id="subbox">
<p>Bye</p>
</div>
</div>
<p>Boo</p>
</body>
</html>
And you only wanted the paragraphs inside box, you could specify a context
to the search:
$('p','#box'); // only find paragraphs inside of #box
Or you could use functions such as find
or children
:
$('#box').find('p'); // will return both the paragraphs inside of #box
$('#box').children('p'); // will only return the immediate children
Instead of having really long selectors I usually like to separate the search terms as much as possible because a) I think it makes more sense to think of it this way, b) it helps jQuery out with parsing so it is slightly faster. So whenever I only want to search part of a document, I sometimes use a combination of the context and the additional functions:
$(myelement, mygreatercontext).find(myrefinedsearch);