Rishabh More Rishabh More - 2 months ago 30
CSS Question

Uncaught TypeError: Cannot read property 'style' of undefined HTML

I've created a section for Slideshow of images in my page!! I've written its HTML, CSS and Javascript for it as well, however when I run it in chrome, I get this javascript error:

Uncaught TypeError: Cannot read property 'style' of undefined

showSlides @slideshow.js:24

(anonymous function)@slideshow.js:2

The slideshow frame is working and visible, but the first image of the slideshow is not appearing automatically. I have to click the forward or reverse buttons to actually view the pictures!! i m guessing its because of this javascript error

Can Anyone help me fix this Javascript Error??

Here are the codes of my HTML, CSS and Javascript file:

HTML part:

<div class="slideShow" style="height: 700px;">
<script type="text/javascript" src="../Data/Scripts/Java/slideshow.js"></script>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_01.jpg">
</center>
<div class="text">Caption Text</div>
</div>

<div class="mySlides fade">
<div class="numbertext">2 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_02.jpg">
</center>
<div class="text">Caption Two</div>
</div>

<div class="mySlides fade">
<div class="numbertext">3 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_03.jpg">
</center>
<div class="text">Caption Three</div>
</div>

<div class="mySlides fade">
<div class="numbertext">4 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_04.jpg">
</center>
<div class="text">Caption Four</div>
</div>

<div class="mySlides fade">
<div class="numbertext">5 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_05.jpg">
</center>
<div class="text">Caption Five</div>
</div>

<div class="mySlides fade">
<div class="numbertext">6 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_06.jpg">
</center>
<div class="text">Caption Six</div>
</div>

<div class="mySlides fade">
<div class="numbertext">7 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_07.jpg">
</center>
<div class="text">Caption Seven</div>
</div>

<div class="mySlides fade">
<div class="numbertext">8 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_08.jpg">
</center>
<div class="text">Caption Eight</div>
</div>

<div class="mySlides fade">
<div class="numbertext">9 / 9</div>
<center>
<img src="../Data/Images/iphone7_plusReview/img_09.png">
</center>
<div class="text">Caption Nine</div>
</div>

<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
<br>

<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
<span class="dot" onclick="currentSlide(4)"></span>
<span class="dot" onclick="currentSlide(5)"></span>
<span class="dot" onclick="currentSlide(6)"></span>
<span class="dot" onclick="currentSlide(7)"></span>
<span class="dot" onclick="currentSlide(8)"></span>
<span class="dot" onclick="currentSlide(9)"></span>
</div>
</div>


CSS part:

* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;margin:0}
.mySlides {display:none}

/* Slideshow container */
.slideshow-container {
max-width: 1010px;
min-height: 500px;
position: relative;
margin-top: 50px;
margin-bottom: auto;
margin-left: auto;
margin-right: auto;
}

/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}

/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}

/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
top: 0;
}

/* The dots/bullets/indicators */
.dot {
cursor:pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}

.active, .dot:hover {
background-color: #717171;
}

/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}

@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}

@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}


And my slideshow.js Javascript file which supposedly has errors as chrome says

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
showSlides(slideIndex += n);
}

function currentSlide(n) {
showSlides(slideIndex = n);
}

function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}


Chrome says i have error at line no. 2 & 24, but i don't understand what is the error?? Can some please help me fix this error? Any help will be gladly appreciated :)

Answer

use your code within a jQuery .ready function like below

var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
var slideIndex;

$(document).ready(function(){
   slideIndex = 1;
   showSlides(slideIndex);
});
$(document).ready(function(){
   function plusSlides(n) {
     showSlides(slideIndex += n);
   }
});
$(document).ready(function(){
   function currentSlide(n) {
     showSlides(slideIndex = n);
   }
});
$(document).ready(function(){
   function showSlides(n) {
     if (n > slides.length) {slideIndex = 1}
     if (n < 1) {slideIndex = slides.length}
     for (i = 0; i < slides.length; i++) {
         slides[i].style.display = "none";
     }
     for (i = 0; i < dots.length; i++) {
         dots[i].className = dots[i].className.replace(" active", "");
     }
     slides[slideIndex-1].style.display = "block";
     dots[slideIndex-1].className += " active";
   }
});