0

It says that it cannot read property "children" of null at function in my code please help me, Im stuck in here for too long.

I tried Renaming the id and checking misspelled code.

<div id="carousel-slide">
    <img src="image1.jpg">
    <img src="image2.jpg">
    <img src="image3.jpg">
</div>

<script type="text/javascript">
var carousel_index = 0;
function show_slides(){
    var carousel_image = document.getElementById("carousel-slide").children;
    var max = carousel_image.length - 1;
    carousel_index+=1;
    if(carousel_index>max) carousel_index = 0;
    if(carousel_index<0) carousel_index = max;
    for(var i=0; i<=max; i++){
        carousel_image[i].className = "carousel-hidden";
    }
    carousel_image[carousel_index].className = "carousel-show";
    setTimeout(show_slides,4000);
}

show_slides();
</script>

1 Answers1

0

I guess that element is not loaded yet. Try this:

document.addEventListener("DOMContentLoaded", function() {
  show_slides();
});
messerbill
  • 4,582
  • 1
  • 19
  • 33