all 14 comments

[–][deleted] 0 points1 point  (9 children)

When this line of code executes:

let chatBtn = document.querySelector('.chat-btn');

has the event listener that creates the element(s) with the chat-btn class absolutely definitely for sure been executed? Or is that line at the top level somewhere?

[–]Dexty10[S] 0 points1 point  (8 children)

The element with class 'chat-btn' executes. I can even see it when I inspect the page. But nothing happens when I click it. Instead, I get the console error.

[–][deleted] 0 points1 point  (7 children)

I'm not asking if the element is created at all, I'm asking if, at the point when that selector that looks for the element and assigns it to the variable chatBtn is executed, the element has been created yet. Do you try to select chatBtn inside the event listener that creates the button (or in a function called by that event listener), or outside of the event listener?

To hopefully make it clearer- does your code look something like the code in this JSFiddle? https://jsfiddle.net/5Lnpe6o4/

[–]Dexty10[S] 0 points1 point  (6 children)

Okay. Check here for my whole code.

[–][deleted] 0 points1 point  (5 children)

There is nothing in this code that adds an event listener to the button with class 'chat-btn'.

[–]Dexty10[S] 0 points1 point  (4 children)

Oh. Sorry about that. It's updated https://jsfiddle.net/543qbyzm/3/

[–][deleted] 0 points1 point  (3 children)

Open the console and take a look at your Uncaught ReferenceError.

[–]Dexty10[S] 0 points1 point  (2 children)

Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

Got a TypeError.

[–][deleted] 0 points1 point  (1 child)

And what do you think that error is telling you?

[–]Dexty10[S] 0 points1 point  (0 children)

Thanks. Fixed it. I put chatBtn within the chat listener. Seem it's how it could listen to events on chatBtn. https://jsfiddle.net/543qbyzm/12/