0
var btnAdaugaPrajeala = document.getElementById('adaugaPrajeala');
var btnClosePrajeala = document.getElementById('closeAdaugaPrajeli');
var modalPrajeala = document.getElementById('mdl-prajeli');




btnAdaugaPrajeala.addEventListener('click', function(){
modalPrajeala.classList.add('active-modal');
});
btnClosePrajeala.addEventListener('click', function(){
    modalPrajeala.classList.remove('active-modal');
});


var btnAdauga = document.getElementById('adaugaSortiment');
var btnCloseAdauga = document.getElementById('closeAdauga');
var modalSortiment = document.getElementById('mdl-sortimente');

btnAdauga.addEventListener('click', function(){
    modalSortiment.classList.add('active-modal');
});
btnCloseAdauga.addEventListener('click', function(){
    modalSortiment.classList.remove('active-modal');
});

I know it's not a really dry code, I tried to do this for the sake of this post. I can't wrap my head around this issue - the 3rd and 4th event listeners are not working. I tried to swap them and it seems that only the first 2 are working, any ideas?

  • Is there an error in the console?? It is nearly impossible to help you with what you provided. – epascarello Jun 11 '20 at 23:49
  • It actually says - Uncaught TypeError: Cannot read property 'addEventListener' of null, at this line " btnAdauga.addEventListener('click', function(){" – Dorin Andrei Jun 11 '20 at 23:51
  • I'm sorry, I'm a newcomer, and I did not find an answer to this. It's weird that if I swap the declarations and functions between them, always, the first half works. – Dorin Andrei Jun 11 '20 at 23:52
  • There you go, it can't find the element. btnAdauga is null – epascarello Jun 11 '20 at 23:52
  • It still works tho, if it's in the first half. – Dorin Andrei Jun 11 '20 at 23:53
  • Well that one finds the button...... Again, no clue why it is not finding the button. – epascarello Jun 11 '20 at 23:55
  • https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – epascarello Jun 11 '20 at 23:56
  • I guess that might be an issue, however, that button works if it's the first one declared, if the other pack of 3 declarations and functions are first, those one are working. – Dorin Andrei Jun 11 '20 at 23:57
  • followed the link, first option worked like a charm, thank youuu – Dorin Andrei Jun 12 '20 at 00:02
  • Does this answer your question? [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – traktor Jun 12 '20 at 00:08
  • it was actually a really stupid mistake, my bad. I just moved the scripts and got it to work, as in the link provided by epascarello. – Dorin Andrei Jun 13 '20 at 00:21

0 Answers0