I have a code for changing the class of an element onclick using pure Javascript. I was pretty confused when the code did not work as I have used the same code many times before and it worked perfectly. Here is the relevant part of the code -
HTML-
<div id="div" class="blue">.</div>
CSS-
.blue{
width: 500px;
height: 500px;
z-index: 9876543210;
position: absolute;
margin-top: 20%;
margin-left: 20%;
-webkit-transition: all 0.5s ease-in-out;
background-color: #24e;
}
.red{
width: 300px;
height: 300px;
position: absolute;
margin-top: 20%;
margin-left: 20%;
-webkit-transition: all 0.5s ease-in-out;
z-index: 9876543210;
background-color: #e69;
}
JAVASCRIPT-
function a(){
this.classList.toggle('blue');
this.classList.toggle('red');
}
document.querySelector('#div').addEventListener('click', a);
I have also linked the page to the online jQuery ( if that makes any difference ). When I click on the "div" nothing happens.
Please suggest a method to get past this problem.