Answer the question
In order to leave comments, you need to log in
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
document.body.addEventListener('click', (e) => {
const element = e.target.closest('a');
if (element) console.log(element.id);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question