I want to change the color of the button dependently on some condition.
In pseudo code it looks like this:
if(condition){
myelememt.background = "something"
}
But it should be checked and applied before the page is loaded.
Thank you!
I want to change the color of the button dependently on some condition.
In pseudo code it looks like this:
if(condition){
myelememt.background = "something"
}
But it should be checked and applied before the page is loaded.
Thank you!
Since you want it to apply the style before DOM paints, you can use DOMContentLoaded
event, since this event fires as soon as the DOM nodes have finished loading, but before all assets, styles, etc... have been loaded.
Something like this should make it:
const condition = true;
document.addEventListener('DOMContentLoaded', (event) => {
const button = document.querySelector('button');
if (condition) button.style.background = 'red';
});
<html>
<head>
<meta charset="UTF-8" />
<script src="script.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<button>CLICK</button>
</body>
</html>