PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 27, 2022

[FIXED] How to compare e.currentTarget and e.Target?

 October 27, 2022     javascript, jquery     No comments   

Issue

I am getting events on button click and background click. In background click I am getting currentTarget and Target both same but in case of button, I am getting different. How can I compare if they are equal or not so that on equal I can perform some operation?


Solution

event.currentTarget refers to the element that the listener was bound to.

Here in the demo we can see if we do actually click the div (possible because of enormous padding) the the match evaluator e.target === e.currentTarget is in fact true.

document.querySelector('#test').addEventListener('click', (e) => {
  if(e.target === e.currentTarget) console.log('yes');
});
div#test {
  padding: 20px;
  background-color: red;
}

div#test p {
  background-color: blue;
}
<div id="test">
  Try clicking here and then the P
  <p id="test2">Test</p>
</div>



Answered By - Bibberty
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing