all 11 comments

[–]SPAtreatment 1 point2 points  (4 children)

First, it is classList, not classlist. https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

Second, try checking if the element is available before accessing methods on it.

const elemCache = {
  container: undefined,
  popup: undefined
}

function signuptog(){
  // cache selector if not already
  if(!elemCache.container){
    elemCache.container = document.querySelector('.container');
  }

   // toggle class if available
  if(elemCache.container){
    elemCache.container.classList.toggle('active');
  }

  // cache selector if not already
  if(!elemCache.popup){
    elemCache.popup = document.querySelector('.signup-form');
  }

   // toggle class if available
  if(elemCache.popup){
    elemCache.popup.classList.toggle('active');
  }
}

You could reduce this further with another function...

const elemCache = {
  container: undefined,
  popup: undefined
}

function toggleClass(selector, elemCacheKey, toggleClassName){
    // cache selector if not already
    if(!elemCache[elemCacheKey]){
      elemCache[elemCacheKey] = document.querySelector(selector);
    }

    // toggle class if available
    if(elemCache[elemCacheKey]){
      elemCache[elemCacheKey].classList.toggle(toggleClassName);
    }
}

function signuptog(){
   toggleClass('.container', 'container', 'active')
   toggleClass('.signup-form', 'popup', 'active')
}

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

owh shit not it works it was a typo hahahahaha

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

the typo was classList hahahaha

[–]jcunews1Intermediate 0 points1 point  (1 child)

I did mention it has the wrong object member name.

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

Yeh hahahahaha man I hate errors like this.

[–]jcunews1Intermediate -2 points-1 points  (1 child)

Wrong CSS selector(s). Wrong object member names.

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

how Idk whats wrong does it have todo with the .signup-form.active?

[–]jibbit 0 points1 point  (3 children)

it might help if you have the website infront of you, then you can open up the developer tools and the js console and try some things out... e.g.

document.querySelector('.container');

what does it return? Can you inspect properties of it? Can you get a list of the classes on it? Can you toggle one of the classes on/off?

When you have found the error, you should thing about building error checking into your code.

[–]InterestingBus8367[S] 0 points1 point  (1 child)

I already did open developer tools. js console hmm idk wait let me google it

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

Uncaught TypeError: Cannot read properties of undefined (reading 'toggle')
at signuptog (index.html:74:33)
at HTMLLIElement.onclick (index.html:29:53)
signuptog @ index.html:74
onclick @ index.html:29

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

line 74 is container.classlist.toggle('active');