0

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 class="coinboxcontainer"><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>
Michael M.
  • 10,486
  • 9
  • 18
  • 34
  • Does this answer your question? [How to reset the style properties to their CSS defaults in javascript?](https://stackoverflow.com/questions/3506050/how-to-reset-the-style-properties-to-their-css-defaults-in-javascript) – Michael M. Oct 29 '22 at 15:03

2 Answers2

0

This should remove all styles from that element:

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

Resetting styles after some time

const button = document.querySelector('button')

button.addEventListener('click', () => {
  button.style.transform = `translate(${100}px)`
  setTimeout(() => button.style.cssText = null, 1000)
})
<button>click me</button>
Konrad
  • 21,590
  • 4
  • 28
  • 64
0

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)
Mohammed Shahed
  • 840
  • 2
  • 15
  • Thanks a lot for the answer, im trying to implement setTimeout to that so that the styles revert to default automatically but i fail – perrytrademark Oct 29 '22 at 18:05