0

I am trying to remove a class on mouseenter with Javascript. Can people explain whats causing it not to work? I am new to JS. The function works with an Alert so i know the function is working so it must be the remove class line thats causing the problem.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
    <title>Document</title>
 </head>
 <body>
    <div class="container">
       <div class="left-filter">
         <div class="left">
           <h1 class="heading1">hi</h1>
           <a href="" class="landing-btn">Shop</a>
         </div>
       </div>
    </div>

    <script>
    var left = document.querySelector('.left');
    var container = document.querySelector('container');
    left.addEventListener('mouseenter', function(){
    container.classList.remove('.left-filter');
    })
    </script>
    </body>
    </html>
3
  • container.classList.remove('left-filter'); Commented May 15, 2018 at 19:20
  • Still is not removing! Commented May 15, 2018 at 19:21
  • Sorry my mistake it is It is! Commented May 15, 2018 at 19:23

2 Answers 2

2

container.classList.remove('.left-filter'); should be container.classList.remove('left-filter');

you only mention the name of the class in remove to remove the class. Not with .

Moreover document.querySelector('container'); should be document.querySelector('.container');

On a side note. If there are more than one elements with class .container, document.querySelector('.container') will only select the first one.

If you want to attach events to all of those elements, you'll have to use document.querySelectorAll('.container') , this will give you the node list and you'll have to loop through this list to attach events to all of the nodes

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for explanation, but this is still not working.
Apologies, it is!
@Blue Glad to know that :) . Consider accepting the answer if it resolved your issue
Did it there! Thanks again
2

Your selector for container is wrong. You are missing a . before the selector:

var container = document.querySelector('.container');
                                        ^
                                        |
                                        ---- This dot was missing

The dot indicates that you are selecting a element by the class. Without it, javascript was searching for a element named container.

Also the line that removes the class is wrong, the . is not needed here:

container.classList.remove('left-filter');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.