Sign in
Log inSign up

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?

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct