I am learning JS and am trying to simply change the background color of a div when clicking on a button and wonder why my code isn't working.
Maybe somebody can take a quick look at the code below:
let btnLeft = document.getElementsByClassName("left");
let btnRight = document.getElementsByClassName("right");
let ad = document.getElementById("ad");
btnLeft.addEventListener("click", changeTheBg());
btnRight.addEventListener("click", changeTheBg2());
function changeTheBg(){
ad.style.backgroundColor = "green";
};
function changeTheBg2(){
ad.style.backgroundColor = "pink";
};
#ad {
width: 400px;
max-width: 500px;
height:200px;
background-color: red;
border-radius: 20px;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="/style.css">
</head>
<body>
<div id="ad"></div>
<div id="controls">
<button class="left">Left
</button>
<button class="right">Right
</button>
</div>
<script src="script.js"></script>
</body>
</html>