P
P
Pavel Beloborodov2020-05-01 04:29:31
JavaScript
Pavel Beloborodov, 2020-05-01 04:29:31

Why event.target doesn't match the clicked element?

https://codepen.io/pawel-beloborodow/pen/wvKrWjo?e...

In the condition if I click, I should get the element I click on. And I get it.
But if I compare it with myself, I get false why.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stepan Krapivin, 2020-05-01
@bekingem

because it event.targetreturns native HTMLElementand $('.test') is a jquery wrapper that is not a DOM element.
It is checked like this:

$(document).on("click", event => {
  let test = event.target;
  console.log(
    test instanceof HTMLElement, // true
    $('.test') instanceof HTMLElement // false
  )
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question