3

In this piece of code:

 $(function() {
 $("div").click(function() {
 $(this).before("<p>Hi</p>");
 });
 });

What does 'this' actually refer to? Does it refer to the whole list of divs, or just to the specific one clicked? And how can I know?

Also, would it matter if I wrote this without the jQuery sign or are they the same?

asked Apr 24, 2013 at 11:14
3
  • 2
    Don't forget to make use of console.log to check the value of any variables you aren't sure of, it will save you a lot of time Commented Apr 24, 2013 at 11:18
  • Thanks, I'm new to jQuery and that didn't come to mind. Commented Apr 24, 2013 at 11:25
  • possible duplicate of jQuery $(this) vs this or may be $(this) Commented Apr 24, 2013 at 11:29

8 Answers 8

5

Within the click handler, this is a reference to the single DOM Element which fired the event—in this case the div element. By wrapping it in $() you create a jQuery reference to that div element (a jQuery object containing that single element, allowing you to call jQuery methods on it.

$(function() {
 $("div").click(function() {
 $(this).before("<p>Hi</p>"); // 'this' is an HTMLDivElement
 });
});
answered Apr 24, 2013 at 11:16
Sign up to request clarification or add additional context in comments.

Comments

3

this is the DOM object, whereas $(this) is the jQuery wrapper around same.

When using this, you can call DOM methods on it, but not jQuery methods. When using $(this), you can call jQuery methods on it, but not DOM methods.

answered Apr 24, 2013 at 11:16

Comments

2

this is always the specific DOM node with which you're interacting, wherease $(this) is the same DOM node, but wrapped in a jQuery object.

The difference is that this can be used with plain JavaScript/DOM API (and $(this) cannot), whereas, if you need to use jQuery methods (hide(), show(), etc...), then you'll need to use $(this) (and not this).

answered Apr 24, 2013 at 11:16

Comments

2

this is just a reference. The language reads it as taking in the most currently active element.

It can be used throughout your code to enable many tasks to take a range of variables that are dependent on the current session.

In many object-oriented programming languages, this (or self) is a keyword which can be used in instance methods to refer to the object on which the currently executing method has been invoked.

take a look at this.

Matt
75.5k26 gold badges156 silver badges181 bronze badges
answered Apr 24, 2013 at 11:16

Comments

1

$(this) refers to the jQuery object created from the selector you attached the event handler too. Any div element the event is performed on becomes this along with the added properties and methods created from being a jQuery object

answered Apr 24, 2013 at 11:16

Comments

0

In this circumstance, this refers to the particular HTML element you have clicked.

No you need to put '$' sign before (this), or else it will result in JavaScript errors.

Matt
75.5k26 gold badges156 silver badges181 bronze badges
answered Apr 24, 2013 at 11:14

Comments

0

Copied from Nick Craver's answer:

  • this - Refers to the DOM element in the handler you're currently on, but this may be another object entirely in other situations, but it's always the context.

  • $this - Usually created by var $this = $(this) a cached version of the jQuery wrapped version for efficiency (or chain off $(this) to get the same in many cases).

  • $(this) - The jQuery wrapped version of the element, so you have access to all its methods (the ones in $.fn specifically).

answered Apr 24, 2013 at 11:18

3 Comments

Next time you copy and paste someone elses answer, I would be careful to make it explicitly clear your answer is a copy and paste; plagiarism is taken very seriously on Stack Overflow (meta.stackexchange.com/questions/78658/…).
i apologize for my error, i copy and pasted nick Craver but i also wrote that was his answer
I appreciate there was a link to his answer in your post, but like I said; you need to make it explicitly clear that your answer is copied from his; otherwise it could be interpreted as a link to further reading.
0

Using jQuery $(this)[0] is the same as using this in Javascript.

So $(this)[0] == this.

daniula
7,0384 gold badges36 silver badges49 bronze badges
answered Apr 24, 2013 at 11:18

2 Comments

... but what is this ;)
Obviously this is this, but clearly different to this. Duh!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.