Tab closes but should be open by default

Im having a small tab menu in a website. The one tab have a id
id="defaultOpen"

However it does not stay open.

 // Get the element with id="defaultOpen" and click on it
  document.getElementById(newLocal).click();

Here is the codepen:

p.s is optimised for mobile not yet for normal screens

When is your script actually bring loaded? You want to affect the DOM when the page loads, but when that script runs (immediately after the script file has read), has the DOM loaded?

Consider adding a defer attribute to the script tag, or moving it to the very end of the body (once the DOM had loaded)

. DevDocs

1 Like

He Snow script is in an external functions document loaded in the header.

I tested to do the code inline in the bottom of the document and then it works.
But loading the code in a separate file breaks the code.

The sollution with the defertag worked.

Thank you for pointing that out to me.

1 Like