How do I click on the background to change blue to red and red to blue? What's wrong with my code? Help, thank you
##</body>
<script>
function te(){
var a = document.getElementById('test1');
if(a.id == 'test1'){
a.id = 'test2';
}else
a.id = 'test1';
}
</script>
</html>
You can use addinventlisner() to listen for click events, initialize it to red, change it to blue when you click it for the first time, and then turn it to red when you click it again
A is undefined when executed for the second time. Just put var a = document.getElementById('test1'); outside the function
Shouldn’t just change one ID, if the match is successful, another one will be displayed