D
D
Denis Davidov2020-01-15 22:51:56
JavaScript
Denis Davidov, 2020-01-15 22:51:56

Error in console "Cannot read property 'addEventListener' of null", how to solve?

The console swears at my code.
I’m just learning the language and after reading I realized that everything seems to be in DOMContentLoaded, they say it loads the page and layout but does not have time for individual files, and on the basis of this there is a conflict.
But after reading something, I did not fully understand how to fix it.
My code:

document.addEventListener('DOMContentLoaded', () => {
  const cartBtn = document.getElementById('#cart');
  const openCart = () => {
    cart.style.display = 'flex';
  };
  cartBtn.addEventListener('click', openCart);
});

Swears at "cartBtn.addEventListener('click', openCart);" and throws "Uncaught TypeError: Cannot read property 'addEventListener' of null at HTMLDocument."
Please help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2019-04-03
@ThunderCat

1) judging by the fact that all sorts of date attributes are hung on the form - something third-party is already using it, look in the inspector what hangs on this form from the listeners.
2) try

$('#form88826033').submit(function(e) {
// я бы еще попробовал $('.t-form_inputs-total_3').submit(function(e) {
        e.preventDefault();
        console.log(this);
        _rc('send', 'order', {...

and see what is falling into the console.

A
Andrey Suha, 2020-01-15
@unkrt

The code should not be posted as a picture. In getElementById, you don't need to use # in front of the selector.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question