albangt (16) [Avatar] Offline
#1
in chap4, bamboo.html; how to explain the two parameters in selector, such as following:
$('div',$(this).parents('div:first'))

thanks a lot
bear.bibeault (675) [Avatar] Offline
#2
Re: a problem about selector in chap4
The second parameter to the $() function defines a context within which to search. In other words, it identifies a sub-tree of the DOM to look within, rather than searching the whole DOM for matches.
mraj (104) [Avatar] Offline
#3
Re: a problem about selector in chap4
Hi albangt,
You might want to have a look at http://docs.jquery.com/Core/jQuery#expressioncontext
Read what's there, but then click on the "Examples" tab to see two examples that use the second parameter.

These "contextualized" selectors are mentioned very briefly on p. 44 of the book.
(Bear, can I suggest that the second edition of the book add a new section in chapter 2 to discuss contextualized selectors? I think they warrant more discussion and some examples earlier in the book.)

Contextualized selectors are useful for a number of reasons: 1) they make your jQuery code run faster, since only a part of the DOM (not the entire page/DOM) needs to be examined. 2) if you have repeating chunks of HTML code, but you only want to select elements from, say, chunk 2. If the chunks each have div with a unique id:

$('some selector','#id2') -- only selects elements from chunk 2.

3) relative searching -- e.g., I'm "here" (say inside a click event that has fired), and now I want to find some elements relative to where I am -- i.e., relative to where the user clicked. This is what the Bamboo example is doing. The second parameter is limiting the selector search to an area of the DOM near where the user clicked.
bear.bibeault (675) [Avatar] Offline
#4
Re: a problem about selector in chap4
Already there.
albangt (16) [Avatar] Offline
#5
Re: a problem about selector in chap4
Thanks a lot