what's the difference between this and $(this)?

On 2/15/07, Klaus Hartl <[EMAIL PROTECTED]> wrote:

Alexandre Plennevaux schrieb:
> Shouldn't it be
>     this.click(function(){ alert(this.attr("id");) });
>
>

No, this is messing up with "this" even more ;-)

Either:

$(this).click(function() {
     alert(this.id);
});

Or:

$(this).click(function() {
     alert($(this).attr("id"));
});

$(this).attr("id") doesn't make much sense of course, because it's the
same as this.id. But if for some reason you'd need something like this
I'd exploit the power of closures and avoid multiple creations of the
jQuery objects:

var $$ = $(this);
$$.click(function() {
     alert($$.attr("id"));
});

I got into the habit to store $(this) in a variable named $$ whenever
required.


-- Klaus


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/




--
Daniel Eastwell

Portfolio and articles:
http://www.thoughtballoon.co.uk

Blog:
http://www.thoughtballoon.co.uk/blog
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to