Articles

$this vs $(this) in jQuery

In jQuery on October 11, 2011 by patrellery


How many times we try to use this and we need to use $(this) and vice versa? Well here’s a quick explanation on how to differentiate these two objects

Using this code as an example:

$("#orderedlist").find("li").each(function(i) {
$(this).append( " BAM! " + i );
});

$("#reset").click(function() {
$("form").each(function() {
this.reset();
});
});

$() is the jQuery constructor function.
this is a reference to the DOM element of invocation.

In other words, passing the this in $() as a parameter so that you could call jQuery methods and functions like append whereas this it’s the element itself.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: