0

I have this code for toggling class using pure JavaScript that I found online and it is not working when I am using it in an offline website my code is -

<!DOCTYPE html>
<html>
<head>
<script>
function classToggle() {
    this.classList.toggle('class1');
    this.classList.toggle('class2');
}
document.querySelector('#div').addEventListener('click', classToggle);
</script>
<style type="text/css">
.class1 {
    color: #f00;
}

.class2 {
    color: #00f;
}
</style>
</head>
<body>
<div id="div" class="class1">click here</div>
</body>
</html>

any help would be appreciated

0

5 Answers 5

1

Move the script below the div you are looking for in the source code.

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

1 Comment

thank you this worked! now i would like to add transition to it can you suggest any suitable though i know it will be a line code something of this sort - .something.style.cssText ="-webkit-transition:0.6s ease-in-out;";
0
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.class1 {
    color: #f00;
}
.class2 {
    color: #00f;
}
</style>
</head>
<body>
<div id="div" class="class1">click here</div>
<script>
function classToggle() {
    this.classList.toggle('class1');
    this.classList.toggle('class2');
}
document.querySelector('#div').addEventListener('click', classToggle);
</script>
</body>
</html>

Comments

0

You cannot manipulate the dom before it is ready.

So either load the script that adds the handler at the end of the body tag, or use the DOMContentLoaded event.

document.addEventListener("DOMContentLoaded", function(event) {
   console.log("DOM fully loaded and parsed");
});

Comments

0

Try adding the event handler after the div has rendered - for example in the onload event

Live Demo

<!DOCTYPE html>
<html>
<head>
<script>
function classToggle() {
  if (!this.classList) return; // no support
  this.classList.toggle('class1');
  this.classList.toggle('class2');
}
window.onload=function() {
  document.getElementById('div').onclick=classToggle;
}
</script>
<style type="text/css">
.class1 {
    color: #f00;
}

.class2 {
    color: #00f;
}
</style>
</head>
<body>
<div id="div" class="class1">click here</div>
</body>
</html>

Comments

0

codepen demo

//vanilla js -- toggle active class 
// el = object containing the elements to toggle active class and the parent  element 
var el = {
  one: document.getElementById('one'),
  two: document.getElementById('two'),
  three: document.getElementById('three'),
  hold: document.getElementById('hold')
};

 // func = object containing the logic 
 var func = {
     toggleActive: function(ele) {
     ele = event.target;
     var hold = el.hold.children;
     var huh = el.hold.children.length;
     var hasActive = ele.classList.contains('active');
     for (i = 0; i < huh; i++) {
         if (hold[i].classList.contains('active')) {
            hold[i].classList.remove('active');
         }  
     }
     if (!hasActive) {
         ele.classList.add('active');
     }
   }
 };

 //add listeners when the window loads 
 window.onload = function() {
    var holdLen = el.hold.children.length;
    for (i = 0; i < holdLen; i++) {
        el.hold.children[i].addEventListener("click", func.toggleActive);
    }

 };

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.