Skip to content

Instantly share code, notes, and snippets.

@yukeehan
Created May 29, 2018 02:53
Show Gist options
  • Save yukeehan/ecf3041bac19703c4f71b602e41a2826 to your computer and use it in GitHub Desktop.
Save yukeehan/ecf3041bac19703c4f71b602e41a2826 to your computer and use it in GitHub Desktop.
use addEventListener to listen the click and change the background color
<!DOCTYPE html>
<html>
<head>
<title>color changer</title>
<style type="text/css">
button{
font-size: 20px;
margin: 50px;
}
.ispink{
background: pink;
}
</style>
</head>
<body>
<button>click me!</button>
<script type="text/javascript" src="colorchanger.js"></script>
</body>
</html>
var button = document.getElementsByTagName("button")[0];
// var ispink = false;
// button.addEventListener("click", function(){
// if(!ispink){
// document.body.style.background = "pink";
// }
// else{
// document.body.style.background = "white";
// }
// ispink = !ispink;
// });
button.addEventListener("click",function(){
document.body.classList.toggle("ispink");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment