R
R
rinaz222020-05-06 17:29:09
JavaScript
rinaz22, 2020-05-06 17:29:09

How to get the id of the link on click, not the id of the child elements?

Hello! I call the function on click body. When I click on blocks, the click does not work (as it should), but when I click on one link, the click is considered as a click on the body. How can I fix

this Here is the link code:

<a href="#" id="block"><i class="fa fa-home"></i> <span>Текст <span id="id">текст 2</span></span></a>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Pastukhov, 2020-05-06
@tyllo

document.body.addEventListener('click', (e) => {
  const element = e.target.closest('a');
  if (element) console.log(element.id);
});

F
Fenion, 2020-05-10
@Fenion

$(document).click(function(e){
  console.log(e.target.id);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question