Is it possible to create a button that, when clicked, changes color to green and then reverts back to its original style after 3 seconds?
I am able to change the button color using an onClick event in my script. However, I encounter scope errors when trying to implement a function that returns the button to its normal colors.
Javascript:
<script type="text/javascript>
function onClickDelayEvent () {
function changeColor(elem) {
elem.style.background = 'green';
elem.style.color = 'white';
}
function revertColor(elem) {
elem.style.background = '';
elem.sytle.color = '';
}
setTimeout(revertColor,3000);
}
</script>
HTML:
<button id="copyButton" onclick="onClickDelayEvent(this)">Copy</button>
The issue arises with the newColor function not executing and triggering an "elem" undefined error in the normalColor function.
If I simplify the code to only change the style once, it works perfectly. The challenge lies in reverting it back to its original state.