Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am currently working through this tutorial: Getting Started with jQuery

For the two examples below:

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

Notice in the first example, we use $(this) to append some text inside of each li element. In the second example we use this directly when resetting the form.

$(this) seems to be used a lot more often than this.

My guess is in the first example, $() is converting each li element into a jQuery object which understands the append() function whereas in the second example reset() can be called directly on the form.

Basically we need $() for special jQuery-only functions.

Is this correct?

like image 930
Kevin Wu Avatar asked Jun 27 '09 00:06

Kevin Wu


People also ask

What's the difference between the words this and that?

This is a pronoun and determiner used to identify someone or something near to the speaker. That is a pronoun and determiner used to identify someone or something at a distance to the speaker.

What is difference between this & these?

This is used with singular or uncountable nouns (i.e. this egg or this music). These refers to plural nouns (i.e. these cookies). When the noun is omitted after this and these, they become pronouns (i.e. turn this off when you leave). Demonstratives are words we use to indicate nouns in a sentence.

How do you teach difference between this and that?

"This Is" and "That Is"Teacher: "This is a pencil." (Stress "this" while holding up the pencil in your hand.) Teacher: "That is a book." (Stress "that," pointing to a book somewhere in the classroom.)

What's the difference between A and à?

The words a and à are grammatical homophones, i.e. they do not have the same grammatical function in the sentence. - a comes from verb avoir conjugated to the indicative present : il a. - à is a preposition. The best way to do their distinction is to change the sentence to another tense such as imperfect.


3 Answers

Yes you only need $() when you're using jQuery. If you want jQuery's help to do DOM things just keep this in mind.

$(this)[0] === this

Basically every time you get a set of elements back jQuery turns it into a jQuery object. If you know you only have one result, it's going to be in the first element.

$("#myDiv")[0] === document.getElementById("myDiv");

And so on...

like image 116
Spencer Ruport Avatar answered Oct 22 '22 20:10

Spencer Ruport


$() is the jQuery constructor function.

this is a reference to the DOM element of invocation.

So basically, in $(this), you are just passing the this in $() as a parameter so that you could call jQuery methods and functions.

like image 371
Reigel Avatar answered Oct 22 '22 19:10

Reigel


Yes, you need $(this) for jQuery functions, but when you want to access basic javascript methods of the element that don't use jQuery, you can just use this.

like image 92
Alex King Avatar answered Oct 22 '22 18:10

Alex King