My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to revert CSS to default using JS?

Dandy Cheng's photo
Dandy Cheng
·Sep 18, 2017

For example, I have the following code:

div{
    position:relative;    
    width:100px;
    height:100px;
    top:50px;
}

Then, with JS:

let div = document.getElementsByTagName('DIV')[0]
let n = 0

div.onclick = ()=>{
   if(n == 0){
      div.style.top = '100px'
      n++    
   }else{
      div.style.top = '50px'
      n = 0
   }
}

As you can see, I need to manually reset the div to it's initial position. Is there anyway I can reset to it's initial position without doing the method above?