Storing references to HTML elements in JS, what’s the convention?

In my app.js file I do a lot of document.getElementById(‘some-element’) calls. I think it would be more readable and maintainable to store the result of this call to a variable and then access the element through it whenever I need to. What’s the common way to do this in vanilla JS so that they’re available in all functions? Do I need to create global variables?

Source: JavaSript – Stack Overflow

November 21, 2021
Category : News
Tags: DOM | javascript

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.