It is clear, but you're misunderstanding the concept. I'll use a class selector to exemplify it:
$('#people .name') is the same as $('#people .name', document) .context = [object HTMLDocument] $('.name', '#people') or $('.name', $('#people')) will simply translate to $('#people').find('.name'), or $('#people .name'), where the context is the *document* object. The context only changes if you pass a HTMLElement as the second argument, then the native methods will be called on that node, not the document: $('.name', $('#people')[0] ) .context = [object HTMLDivElement] For example (using common DOM methods): $('#people') == $('#people', document); what's happening is equivalent to document.getElementById('people'); now in this case $('div', document.getElementById('people')) what is actually happening is [#people node].getElementsByTagName('div'); In other words, jQuery objects and strings passed as a context will act as filters, but the context will still be the document. The context will only change if you pass in a DOM node. It's a bit counter-intuitive but that's the way it works right now. cheers, - ricardo On Mar 25, 9:30 pm, DBJ <dbj...@gmail.com> wrote: > This is, of course, true and fine. Exactly the purpose of the context. > To define the starting node BELLOW which it will look further. Fine. > It is only that this is not what current documentation is saying : > > http://docs.jquery.com/Core/jQuery#expressioncontext > > At least NOT clearly and NOT unambigously... > > On top of that jQuery.selector and jQuery.context in this case are not > what was originaly passed but what was processed inside jQ > > So: > > $("[name]","#people") which is equivalent to $("#people > [name]"). > > Results in : > > selector := "'#people [name]" // space in the middle > context := DOMDocument > > After call to $() finsihes, and plugins can start using this properties. > Above is wrong and should be : > > selector := "[name]" > context := "#people " // space on the end > > This might seem as some nitpicking, but it is actually extrmely important. > Because if context is always given , jQuery enabled pages will be faster. > In some case much faster. So the context syntax should be crystal > clear and consistent. > > In my mind context (as I said but I will repeat ;) should be only dom > node, or single id string. If given by user. So these will be only > three valid forms: > > $("'#people") // dom document default context > $("[name], "#people") //single id > $("[name]", domNode ) // element given as context > > I see no reason for anything else to act as context. > This would be (as I also already said ;) excellent start for people to > understand how to structure their pages and why. And why are properly > strucutred pages faster to traverse with jQuery( selector, context ) > calls. > > 2009/3/25 Julian Aubourg <aubourg.jul...@gmail.com>: > > > > > $("[name]","#people") is equivalent to $("#people [name]"). > > Please note the spacing. > > It then means every element with a name attribute BELOW the element of id > > "people". > > $("#people[name]") means the element of id "people" with a name attribute. > > So, YES, this is quite different. > > 2009/3/25 DBJ <dbj...@gmail.com> > > >> @Daniel > > >> You confuse me ;o) > > >> Aa I said : A quick sample I have made (quickly) is here: > >>http://jsbin.com/avowu/edit > > >> Please use it now. It is extremely simple and it shows what is going > >> on inside jQuery, regarding : this.selector and this.context. > > >> So when I say: $("[name]", "#people"); I can (and you) can see that > >> inside jQuery selector (aka this.selector) is : "'#people[name]" > > >> So I am not "thinking" on this one I am just passing it on. This is > >> also very confusing: > > >> > Thinking that $("[name]", "#people"); is the same as $("#people[name]"); > >> > is wrong on multiple levels. > > >> This is how I understood jQuery documentation describes the context, > >> but this is not > >> apparently how this.selector value is defined inside ... This might be > >> "wrong on multiple levels" bi this is the value of this.selector , as > >> we can clearly see. > > >> Also you say : > > >> > $("[name]", $("#people")); actually means $("#people [name]"); > > >> Are you actually saying this is the proper usage? Ok, but why is than > >> the "improper" usage allowed? If it is imporper that is. But the > >> question (for the last snippet) is what is then the value of the > >> this.slector going to be, when used by a plugin ? Currently it is : > >> "#people [name]" > > >> Also, why are we in this mess ? Can we have consistent documentation > >> and implementation that is folllowing it? Something like : > > >> "Context type can be a dom node or an jQuery instance. Everything else > >> throws an exeception if passed as a context to the jQuery() function" > > >> This would be a really good start to the discussion on what is > >> actually a "context" ;o) > > >> Regards. > > >> 2009/3/25 Daniel Friesen <nadir.seen.f...@gmail.com>: > > >> > Context is either a dom node, a document, or recently a jQuery object > >> > with the like. Context is merely used for establishing a dom node as a > >> > parent, and in the case of a document is necessary to know what document > >> > to use to create new nodes for. > >> > Thinking that $("[name]", "#people"); is the same as $("#people[name]"); > >> > is wrong on multiple levels. $("[name]", $("#people")); actually means > >> > $("#people [name]"); and the contex is not meant to be used that way. > >> > In fact trying to think that way makes things confusing and reduces code > >> > readability because you are mixing up order. If you want to build > >> > queries like this then I'd recommend you push for the inclusion of my > >> > alternate proposed format: > >> > $("#selector", "[selector]", ".selector", ...); > >> > Which makes proper sense since you are building up selectors in order > >> > (even has potential performance benefits if someone is adventurous since > >> > I'd explicitly say to not rely on any consistent return for .end() when > >> > using it). It's even more useful when you throw widget objects into the > >> > mix. > > >> > ~Daniel Friesen (Dantman, Nadir-Seen-Fire) > > >> > DBJDBJ wrote: > >> >> My question/comment : What is a CONTEXT LOGIC in jQ ? I think it > >> >> should be : dom node inside which is the result set to be found? > >> >> The dom node inside which is jQuery to use the selector given. Default > >> >> is document. > > >> >> If I read it right , jQ doc says that context type can be (only?) dom > >> >> node or jQuery instance. So I expected that if I give context as a > >> >> string it will be takens as selector for the jQuery, which will > >> >> eventualy spill out a single dome node to be used as a context. And > >> >> that > >> >> way one can speed up her code, because querying inside the known dom > >> >> element must be quicker than querying inside the whole document, each > >> >> time. finding on the branch is faster that finding from the root of > >> >> the tree. > > >> >> But it seems that if string is given as a context , the selector get's > >> >> it as a prefix and context is a dom document ? So : > > >> >> $("[name]", "#people") is the same as saying $("#people[name]", > >> >> document ) , because jQ makes it into this. > > >> >> While one might expect the above to be translated to : $("[name]", > >> >> document.getElementById("people") ) , which could considerably speed > >> >> up the slection ... > > >> >> A quick sample I have made (quickly) is here: > >> >>http://jsbin.com/avowu/edit > > >> >> // context is correctly reported by jQ > >> >> $("[name]", document.getElementById("people")).formula( log ) ; > > >> >> // this.context is reported by jQ as HTMLDocument ? > >> >> // this.selector is prefixed with context > >> >> $("[name]", "#people") ; > > >> >> Is this "by design" ? Maybe jQ does this : > > >> >> 1 :: jQuery("#people").find("[name]") > >> >> 2 :: this.context = "#people" + "[name]" > > >> >> So that plugins can see the context which actually was used ? So the > >> >> line : > >> >> $("[name]", "#people") > >> >> after all IS actually making things faster ? And I am (or I was) > >> >> confused with the value of this.context and this.selector, available > >> >> to me inside a plugin ? But then I think I can;t be right here, > >> >> because it would be much better to have the selector and context, as > >> >> originaly > >> >> given by the user : > > >> >> 1 :: this.context = "#people" ; this.selector = "[name]" ; > >> >> 2 :: jQuery(context).find(selector) > > >> >> After this plugins will see the original selector and context. > >> >> Provided my assumptions are correct. > > >> -- > >> Dusan Jovanovic > >>http://dbj.org > >> ------------------------- > >> "html e-mail is expensive" ™ > > >> This email originates from Dusan B. Jovanovic. It, and any > >> attachments, may contain confidential information and may be subject > >> to copyright or other intellectual property rights. It is only for the > >> use of the addressee(s). You may not copy, forward, disclose, save or > >> otherwise use it in any way if you are not the addressee(s) or > >> responsible for delivery. If you receive this email by mistake, please > >> advise the sender and cancel it immediately. Dusan B. Jovanovic may > >> monitor the content of emails within its network to ensure compliance > >> with its policies and procedures. Any email is susceptible to > >> alteration and its integrity cannot be assured. Dusan B. Jovanovic > >> shall not be liable if the message is altered, modified, falsified, or > >> edited. > > -- > Dusan Jovanovichttp://dbj.org > ------------------------- > "html e-mail is expensive" ™ > > This email originates from Dusan B. Jovanovic. It, and any > attachments, may contain confidential information and may be subject > to copyright or other intellectual property rights. It is only for the > use of the addressee(s). You may not copy, forward, disclose, save or > otherwise use it in any way if you are not the addressee(s) or > responsible for delivery. If you receive this email by mistake, please > advise the sender and cancel it immediately. Dusan B. Jovanovic may > monitor the content of emails within its network to ensure compliance > with its policies and procedures. Any email is susceptible to > alteration and its integrity cannot be assured. Dusan B. Jovanovic > shall not be liable if the message is altered, modified, falsified, or > edited. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---