[jQuery] Re: what does this selector mean ?
On Oct 18, 2009, at 2:05 AM, Michael Geary wrote: $('div',this) is simply a confusing way of writing $ (this).find('div'). The only reason it exists at all is for "historical reasons": it was added to jQuery before the .find() method existed. Never use $('div',this) in your code. Always use $(this).find('div') instead. It is easier to read and faster too. -Mike Right on! --Karl
[jQuery] Re: what does this selector mean ?
Yes, in fact the snippet you posted shows why $(context).find(selector) is faster. $(selector,context) *calls* $(context).find(selector). By calling $(context).find(selector) directly, you avoid this extra call and the checks that lead to it. Therefore, $(context).find(selector) is faster by a very small constant amount. The improved readability is the more important point. $(selector,context) is backwards from the normal parent-child order in CSS selectors, and it isn't self-documenting the way $(context).find(selector) is. There's no reason for $(selector,context) to exist at all, except for backwards compatibility with jQuery code written in 2006. It should be deprecated. -Mike On Sun, Oct 18, 2009 at 1:53 AM, MorningZ wrote: > > Faster? aren't they just equivalent? > > according to the comments in the library file they are > > // HANDLE: $(expr, [context]) > // (which is just equivalent to: $(content).find(expr) >} else > return jQuery( context ).find( selector ); > > On Oct 18, 2:05 am, Michael Geary wrote: > > $('div',this) is simply a confusing way of writing $(this).find('div'). > The > > only reason it exists at all is for "historical reasons": it was added to > > jQuery before the .find() method existed. > > > > Never use $('div',this) in your code. Always use $(this).find('div') > > instead. It is easier to read and faster too. > > > > -Mike > > > > On Tue, Sep 29, 2009 at 4:24 AM, runrunforest > wrote: > > > > > Hi, > > > > > $('div', this) what does that mean ? >
[jQuery] Re: what does this selector mean ?
Faster? aren't they just equivalent? according to the comments in the library file they are // HANDLE: $(expr, [context]) // (which is just equivalent to: $(content).find(expr) } else return jQuery( context ).find( selector ); On Oct 18, 2:05 am, Michael Geary wrote: > $('div',this) is simply a confusing way of writing $(this).find('div'). The > only reason it exists at all is for "historical reasons": it was added to > jQuery before the .find() method existed. > > Never use $('div',this) in your code. Always use $(this).find('div') > instead. It is easier to read and faster too. > > -Mike > > On Tue, Sep 29, 2009 at 4:24 AM, runrunforest wrote: > > > Hi, > > > $('div', this) what does that mean ?
[jQuery] Re: what does this selector mean ?
$('div',this) is simply a confusing way of writing $(this).find('div'). The only reason it exists at all is for "historical reasons": it was added to jQuery before the .find() method existed. Never use $('div',this) in your code. Always use $(this).find('div') instead. It is easier to read and faster too. -Mike On Tue, Sep 29, 2009 at 4:24 AM, runrunforest wrote: > > Hi, > > $('div', this) what does that mean ? >
[jQuery] Re: what does this selector mean ?
It's a little confusing, but the difference is that the "contextual selector" is a second argument, whereas the "or selector" is a comma within a single argument. So, if we forget about "this" for a moment: $('.myclass, #myid') -- matches all elements that have a class of "myclass" or id="myid" -- both selectors are contained within the same argument (i.e. the same set of quotation marks) $('.myclass', '#myid') -- matches all elements that have a class of "myclass" and are descendants of an element with id="myid" -- two arguments. One way to include "this" in the selection would be to use .add(). For example: $(this).add('div'); -- select this and all div elements. Hope that helps. --Karl Karl Swedberg www.englishrules.com www.learningjquery.com On Oct 17, 2009, at 10:40 PM, lampy wrote: I know Karl's answer is correct, but from the docs ( http://docs.jquery.com/Selectors/multiple#selector1selector2selectorN ) I would've thought the comma meant "select all div elements, and this". Is there any documentation that would help me understand this? On Sep 29, 10:03 pm, MorningZ wrote: oops... that'll teach me to try to make sense first thing in the morning :-) Coffee first THEN post On Sep 29, 9:11 pm, Karl Swedberg wrote: On Sep 29, 2009, at 7:33 AM, MorningZ wrote: Whatever "this" is in that case, it's the context to look for any (jQuery will only select 's that are ancestors of "this" in the DOM tree) Correction: for $('div',this), jQuery will only select s that are descendants of "this" in the DOM tree. --Karl Karl Swedbergwww.englishrules.comwww.learningjquery.com
[jQuery] Re: what does this selector mean ?
I know Karl's answer is correct, but from the docs ( http://docs.jquery.com/Selectors/multiple#selector1selector2selectorN ) I would've thought the comma meant "select all div elements, and this". Is there any documentation that would help me understand this? On Sep 29, 10:03 pm, MorningZ wrote: > oops... that'll teach me to try to make sense first thing in the > morning :-) > > Coffee first THEN post > > On Sep 29, 9:11 pm, Karl Swedberg wrote: > > > On Sep 29, 2009, at 7:33 AM, MorningZ wrote: > > > > Whatever "this" is in that case, it's the context to look for any > > > (jQuery will only select 's that are ancestors of "this" in > > > the DOM tree) > > > Correction: for $('div',this), jQuery will only select s that > > are descendants of "this" in the DOM tree. > > > --Karl > > > > > Karl Swedbergwww.englishrules.comwww.learningjquery.com
[jQuery] Re: what does this selector mean ?
oops... that'll teach me to try to make sense first thing in the morning :-) Coffee first THEN post On Sep 29, 9:11 pm, Karl Swedberg wrote: > On Sep 29, 2009, at 7:33 AM, MorningZ wrote: > > > > > Whatever "this" is in that case, it's the context to look for any > > (jQuery will only select 's that are ancestors of "this" in > > the DOM tree) > > Correction: for $('div', this), jQuery will only select s that > are descendants of "this" in the DOM tree. > > --Karl > > > Karl Swedbergwww.englishrules.comwww.learningjquery.com
[jQuery] Re: what does this selector mean ?
On Sep 29, 2009, at 7:33 AM, MorningZ wrote: Whatever "this" is in that case, it's the context to look for any (jQuery will only select 's that are ancestors of "this" in the DOM tree) Correction: for $('div', this), jQuery will only select s that are descendants of "this" in the DOM tree. --Karl Karl Swedberg www.englishrules.com www.learningjquery.com
[jQuery] Re: what does this selector mean ?
Whatever "this" is in that case, it's the context to look for any (jQuery will only select 's that are ancestors of "this" in the DOM tree)