I am struggling with changing the background color of an html document using a button click.
While colors like "yellow, "blue", and "red" work perfectly, I encounter an issue when trying to use hexadecimal colors such as "#000000".
The if-condition doesn't seem to recognize the hexadecimal color and defaults to the else function instead.
Interestingly, substituting "#000000" with "black" makes the function work as intended and the background turns red.
If you would like to see the code in action, check out the working sample on jsfiddle:
https://jsfiddle.net/c2gv9x01/
<button>COLOR SWITCH</button>
<script>
window.onload=function(){
document.querySelector("body").style.backgroundColor = '#000000';
document.querySelector("button").addEventListener("click", color);
}
function color() {
if (document.querySelector("body").style.backgroundColor == '#000000')
{document.querySelector("body").style.backgroundColor = 'red'; }
else {document.querySelector("body").style.backgroundColor = '#000000';}
}
</script>