DEV Community

Cover image for How to get information of an Element to the console on click in JS

How to get information of an Element to the console on click in JS

acw0415 on July 09, 2022

document.addEventListener("click", (e) => console.log(e.target)) So let me begin with this: I am by no means an expert on javaScript(not yet an...
Collapse
 
thesnowmanndev profile image
Kyle Martin • Edited

Not bad. Has a very specific use case I guess but if it helps you find html elements easier then that’s good. Look into developer.mozilla.org/en-US/docs/W... to help remove an event listener if a condition is met. This will help application performance if you ever do use this in a production app.

I also recommend learning how to separate html markup into template literals assigned to a variable in a JS file. Then having your main JS file import those markup variables, query selecting ids in your index.html and using variableName.innerHTML = importedMarkupVariable to render the html. Check out my vanilla JS small web app for made using this sort of Separations of Concerns. github.com/Thesnowmanndev/Random-S... You can also look at previous small apps I made in that same repo.

Sorry if this reply is not formatted, I am on mobile web browser.

Collapse
 
acw0415 profile image
acw0415

Thanks! I'll definitely check this out! I appreciate the feedback!