I'm trying to create an image rotator in JS. The fade animation I'm applying in CSS only works on element creation, so I'm having to remove the element on each iteration through the loop.
The problem I'm facing is in the title - I can't seem to remove the clientImg, and I can't figure out why... Can anyone help?
The error I'm getting is: Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'.
JS
function clientRotator(){
var section = document.getElementById("clients");
var clientImg = document.createElement("img");
clientImg.setAttribute("id", "rotator");
section.appendChild(clientImg);
clientImg.src = "assets/exxon-mobil.png";
var imgArray = ["assets/shell.png", "assets/bp.png", "assets/talisman.png", "assets/cnr-international.png", "assets/exxon-mobil.png"];
var delaySeconds = 3;
var iteration = 0;
setInterval(function(){
console.log(imgArray[iteration]);
section.removeChild(clientImg);
var clientImg = document.createElement("img");
clientImg.setAttribute("id", "rotator");
section.appendChild(clientImg);
clientImg.src = imgArray[iteration];
if (iteration < imgArray.length-1){
iteration += 1;
}
else {
iteration = 0;
}
}, delaySeconds * 1000)
}
window.addEventListener("load", clientRotator());
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/style.css">
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
</head>
<body>
<section id="header">
<img id="logo" src="assets/logo-new.png" alt="Logo">
</section>
<section id="clients">
<!-- Rotating images go here -->
</section>
<footer>
</footer>
</body>
<script src="scripts/main.js"></script>
</html>