Home > Software engineering >  Javascript Auto Reset CSS Styles
Javascript Auto Reset CSS Styles

Time:10-30

I have a button that changes an element's style. Can I have this button auto-reset the CSS changed, back to default? Here is the code:

<div ><img id="coin1" src="/wp-content/uploads/2022/10/coin.png" alt="box" width="40" height="40">
<a onclick="tosscoin()"><img src="/wp-content/uploads/2022/10/coinbox.png" alt="box" width="40" height="40"></a>
<audio id="audio" src="/wp-content/uploads/2022/10/coinsound.mp3"></audio>
<script>
function tosscoin() {
       document.getElementById("coin1").style.transform = "translatey(-70px)";
       var audio = document.getElementById("audio");
       audio.play();
}
</script>
</div>

CodePudding user response:

This should remove all styles from that element:

document.getElementById("coin1").style.cssText = null

CodePudding user response:

To remove css styles from an element using js Removes all styles.

  1. document.getElementwithSomeMethod.style=null
  2. document.getElementwithSomeMethod.style.cssText=null

Remove a single style

  1. document.getElementBySomeMethod.style.removeProperty(propertyName)
  • Related