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>
1

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for the answer! Though i am looking for a way to do that automatically.. for example after 2 seconds after pressing the button, the styles revert back to normal. Thanks again for your time
Use setTimeout
Yep tried that already with no luck! I will try more! Thanks again!
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)

1 Comment

Thanks a lot for the answer, im trying to implement setTimeout to that so that the styles revert to default automatically but i fail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.