example..
<input type="button" value="click me" id="p1">
<script>
document.querySelector("#p1").onclick = function () {
this.style.backgroundColor = "green";
alert(`My background color is ${this.style.backgroundColor} ??`);
}
</script>
The alert write My background color is green but at the moment of alert the color is gray!!
I know many ways to get around this:
- don't use alert but show a div..
- use setTimeout
- use sleep function with async await (What is the JavaScript version of sleep()?)
..but is a way to be sure that the browser rendering is done?