'this' value in jquery code

Sorry to ask this question, but what does 'this' mean in this code?

(it is in jQuery).

var icon = $('.icon', this); 
+4
source share
2 answers

I understand that he will do something similar to:

 $(this).find('.icon'); 

That is, it will find all the children of the 'this' matching the selector. For example, it can be used as:

 $('.list').each(function () { $('.icon', this).hide(); }); 

As an equivalent:

 $('.list .icon').hide(); 
+3
source

this is a contextual or just parent element:

 var icon = $('.icon', this); 

Here, this refers to an element that contains an element with the icon class.

You can also write it like this:

 var icon = $(this).find('.icon'); 

In fact, you pasted into partial code, here is an example:

 $('#someID').mouseenter(function(){ $('.someClass', this).addClass('myClass'); }); 

In the above code, this refers to an element with id someID .

You can get more information here:

+3
source

Source: https://habr.com/ru/post/1332650/


All Articles