Skip to content
Advertisement

Why is my Javascript toggleClass code not working?

I am trying to add the .open class to a div when the it is click. It is currently not working, and I’m not sure why.

Here is my JS

JavaScript

Here is my HTML

JavaScript

For reference, I am trying to replicate exactly what is happening with the bottom right “have a question” widget on this website https://soldaf.ca/

Thanks,

Advertisement

Answer

The issue appears to be because the syntax you’re using to contain the jQuery code is an Immediately Invoked Function Expression (IIFE), and not a jQuery document.ready handler. This means that your code is running before the DOM has loaded, and therefore the elements you’re trying to bind event handlers to do not exist yet.

To fix the problem, use a standard jQuery document.ready event handler:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement