In other cases, it is better to write the code in a separate js-file and include it on the necessary pages. It is desirable to minimize all code on production sites, and if there are a lot of files with scripts, it would be a good practice to combine such files into one and then connect it to the page. This approach will significantly speed up the loading of your pages. Download speed can be viewed in the browser console in the network tab or use services like Goggle PageSpeed .
It will be correct to register classes for tags, in the class attribute , and access the elements from the js-code placed in the file. Sometimes you can write identifiers, id attribute of the tag. But classes are preferable, as this way you can also use them in css .
Elements (selectors) are accessed using 6 basic methods for finding DOM elements: getElementById , getElementsByName , getElementsByTagName , getElementsByClassName , querySelector , querySelectorAll . Thus, having found an element on the page, you can set a handler for it, delete the element itself, change its properties, etc., that is, give the page interactivity, animate it.
- IT / Misc 08.07.21 How to make a free translation for a website without an API, translate documents in Google Translate
- IT / Misc 06.07.21 How to make a subscription button on a website, a subscriber base and automatic mailing
- Food / Misc 06.07.21 How to quickly cook delicious fried pies with potatoes and onions
- IT / Misc 04.07.21 Caching - create, load and reset. Where to store the cache, methods and types of caching
- IT / Database 03.07.21 Custom NoSQL - storing data in files and not only in a database. Storing settings, small data and caching files