Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

el.classList is undefined

Tags:

I'm trying to add a class to a div in pure JS once a link is clicked (In this case, the link has id #switchlogin). When I console.log my element, I am able to click through the object and see that it currently has two names in its classList. However, when I console.log element.classList, I receive an "undefined". I am unsure why I'm receiving undefined, and why I cannot add my new class name.

The issue is recreated here: https://jsfiddle.net/swf5zc74/

Here is a portion of my HTML:

   <div class="sign_in modal">

      <h1>Get Started!</h1>

      <form action="" method="get">

        <input type="text" name="fname" placeholder="Name" required>
        <input type="text" name="email" placeholder="Email" required>
        <input type="password" name="password" placeholder="Password" required>
        <input type="submit" value="Submit" class="submit">

      </form>

      <h3><a href="#" id="switchLogin">Been Here Before?</a></h3>
    </div>

My JS:

var body = document.getElementsByTagName('body'),
    signIn = document.getElementsByClassName('sign_in modal'),
    logIn = document.getElementsByClassName('log_in'),
    switchLogIn = document.getElementById('switchLogin'),
    switchSignIn = document.getElementById('switchSignin'),
    link = document.querySelector('a');

link.addEventListener("click", function(e){
    if ((e.target || e.srcElement).id == 'switchLogin'){
        console.log(signIn);
        console.log(signIn.classList);
        signIn.classList += ' slideOut';
    }else{
        console.log("nope");
    }
})
like image 692
Angelina Bethoney Avatar asked Oct 14 '15 17:10

Angelina Bethoney


People also ask

What is classList in angular?

The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element. This can then be used to manipulate the class list. Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via element.className .

What is a classList JavaScript?

JavaScript classList is a DOM property of JavaScript that allows for styling the CSS (Cascading Style Sheet) classes of an element. JavaScript classList is a read-only property that returns the names of the CSS classes.

How do you clear a classList in JavaScript?

To remove all classes from an element, set the element's className property to an empty string, e.g. box. className = '' . Setting the element's className property to an empty string empties the element's class list.


2 Answers

signIn is the result of document.getElementsByClassName('sign_in modal') - getElementsByClassName returns a collection - so you have to reference the index you want:

console.log(signIn[0].classList);

Demo: https://jsfiddle.net/swf5zc74/1/

like image 190
tymeJV Avatar answered Sep 28 '22 09:09

tymeJV


document.getElementsByClassName returns an "array-like" collection of elements, even if there is only one element with that class name. To access elements with document.getElementsByClassName, you can use array syntax.

for exampe logIn = document.getElementsByClassName('log_in')[0]

want to add a class to that element?

document.getElementsByClassName('log_in')[0].className += " foo";

like image 32
Jake Avatar answered Sep 28 '22 08:09

Jake