Why is the value of 'selector' different from 'this'

Inside the event handler, why $ (this) returns something else than $ ('. Selector')?

Example:

$('.container').click(function () {
    console.log($(this));
    console.log($('.container'));
});

jsFiddle

When you look in the console, the results are different.

+4
source share
2 answers

this- it is always the element on which the event arose , in other words, which of the elements .containeryou clicked accurately.

eg:.

<div class="container">container1</div>
<span class="container">container2</span>

as Jonathan Lonowski notes, $(".container")selects both elements .container, but this- the one you clicked on either spanor div.

, $(this) JQuery, this javascript.

+9

this .container, .

.container .

+6

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


All Articles