Oh, you're absolutely right, wick... I was just careless in my coding.
Thanks for pointing that out and correcting the example...for my sake
and, especially for GGerri's!

Rick

-----Original Message-----
From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of wick
Sent: Thursday, March 05, 2009 11:58 AM
To: jQuery (English)
Subject: [jQuery] Re: Simple one: difference between varXy.find(':text') and
$(varXy).find(':text')


Rick, as far as I can tell there is something wrong with two of the code
examples you provided.

var mySet = '$(mySet)';   ...this sets the mySet variable to a string
that *looks* like a jQuery selector, but your quotes make it into a useless
string.
$('mySet').find(':text')...    ...this selector looks for a "mySet"
HTML object (which does not exist), again, because of the way you have the
quotes. It doesn't use the mySet variable reference at all.

To answer GGerri's question..

var mySet = $('tr>td:nth-child(2n)');  ...assigns a jQuery object to the
mySet variable.
mySet.find(':text')   ...takes your jQuery object & applies find() to
it.
$(mySet).find(':text')   ...takes your jQuery object, runs it through
the jQuery selector engine again, & applies find().

Both ways work okay, but the 2nd way isn't the best because running a jQuery
object through the selector engine again serves no purpose that I'm aware
of. Normally you'd use the 2nd example only if mySet was a DOM object
reference, not a jQuery object reference.

Regarding GGerri's question about the "this" variable - in jQuery, "this"
refers to a DOM object so you always need to wrap it with the jQuery $(...)
selector if you're going to use jQuery methods on it.

A useful variable naming convention I've seen is to prefix any jQuery object
variables with a dollar sign. It's an easy reminder that the variable is
already a jQuery object. In other words:

var $mySet = $('div h1 a');  ...."$mySet" becomes a jQuery object
$mySet.show(300,function() {
  $(this).fadeIn(); ..."this" is a DOM object, so you need to wrap it with
the jQuery selector });

Hope that helps!

-Wick
http://www.CarComplaints.com


On Mar 5, 7:58 am, "Rick Faircloth" <r...@whitestonemedia.com> wrote:
> Hi, Rayn :handshake: :o)
>
> I think, for shorthand notation (some say for readability, but I think 
> otherwise), some set var's (variables) to represent pieces of code, for
instance:
>
> var mySet = '$(mySet)'
>
> and then use it as:
>
> mySet.find(':text')...
>
> Written in "longhand", it would be:
>
> $('mySet').find(':text')...
>
> When trying to read someone else's code, where this shorthand is use 
> extensively, I just find it hard to decipher, since I have to trace 
> all the var's down to find out what they stand for...
>
> Someone please correct me if I'm wrong...
>
> Rick
>
> -----Original Message-----
> From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] 
> On Behalf Of ggerri
> Sent: Thursday, March 05, 2009 7:31 AM
> To: jquery-en@googlegroups.com
> Subject: [jQuery] Re: Simple one: difference between 
> varXy.find(':text') and $(varXy).find(':text')
>
> Thanks Ryan :handshake:
>
> so  mySet.find(':text').each(...) would be right and
> $(mySet).find(':text').each(...) not? :confused:
>
> In examples I often see (within an each function): $(this).something 
> but also this.something
>
> Still dont get the difference of use :,(
>
> G
>
> ryan.joyce...@googlemail.com wrote:
>
> > mySet is an object or variable, $(mySet) will try to get an element 
> > using the contents of mySet as the selector.
>
> > On Mar 5, 10:04 am, ggerri <gerald.ressm...@ewz.ch> wrote:
> >> Hi there
>
> >> thats an easy one for you ;-)
>
> >> if i do:
>
> >> var mySet = $('tr>td:nth-child(2n)');
>
> >> how do I use mySet? What's the difference between
>
> >> mySet.find(':text')
>
> >> and
>
> >> $(mySet).find(':text')
>
> >> Thanks :-))
> >> GGerri
>
> >> --
> >> View this message in
> >>
context:http://www.nabble.com/Simple-one%3A-difference-between-varXy.find%28
%...
> >> Sent from the jQuery General Discussion mailing list archive at 
> >> Nabble.com.
>
> --
> View this message in
context:http://www.nabble.com/Simple-one%3A-difference-between-varXy.find%28
%...
> Sent from the jQuery General Discussion mailing list archive at
Nabble.com.

Reply via email to