I put this code at the beginning of main.js - all scripts after it stop working. And if at the end of the code - all scripts work. What is it about? Variables do not overlap. This script is working.

var newsbtn = document.querySelector('.news__to-all'); newsbtn.addEventListener('click', function (event) { event.preventDefault(); newsbtn.classList.toggle('news__to-all--show'); }); 

Refinement . This script only breaks scripts from other html pages. Scripts shared with him html-pages work. On another html-page there is the same script, which also lamas the scripts of all the other html-pages, except its own:

 var portfBtn = document.querySelector('.portfolio__to-all'); portfBtn.addEventListener('click', function (event) { event.preventDefault(); portfBtn.classList.toggle('portfolio__to-all--show'); }); 

In separate files, all scripts are working.

  • one
    This may be if an error occurs in this code. Most likely the newsbtn element newsbtn not found and you are trying to hang an event on null - user3127286
  • that is, newsbtn is not in the markup? But news__to-all seems to be in the markup ... - Millionaire

1 answer 1

Probably, in the markup to this script there are no elements with the class news__to-all , which is why querySelector returns null and everything falls on addEventListener .

  • wow, right. And no "psychic battle" - ru.stackoverflow.com/questions/720904/… - Igor
  • on another html page, of course, are missing. Or do I need to do my own concatenation of scripts for each page? - Millionaire