How to add EventListener to href with id "logout"? I do not want to add all EventListener to add anchors at once.
<a href="#" class="sub-menu-link">
<img src="images/profile.png">
<p>Edit Profile</p>
<span>></span>
</a>
<a href="#" class="sub-menu-link">
<img src="images/setting.png">
<p>Settings & Privacy</p>
<span>></span>
</a>
<a href="#" class="sub-menu-link">
<img src="images/help.png">
<p>Help & Support</p>
<span>></span>
</a>
<a href="#" class="sub-menu-link" id="logout">
<img src="images/logout.png">
<p>Logout</p>
<span>></span>
</a>
3
Answers
You can add like so.
To put in another way, if you want to add event listeners only to specific anchor tags without adding them all at once, then you can create a common ancestor element and do event-delegation.
This way, you only need to attach one event listener which is to the common ancestor element, even if you add more anchor tags in the future.