I am unable to resolve this error; I’ve been “fighting” to solve it for two days.
This is the error:
215: Uncaught TypeError: Cannot read properties of undefined (reading ‘style’) at showSlides
JavaScript
x
65
65
1
var slideIndex = 1;
2
3
4
var outerXmlhttp = new XMLHttpRequest();
5
var url = "https://wjko8t4509.execute-api.us-east-2.amazonaws.com/books";
6
outerXmlhttp.onreadystatechange = function () {
7
var innerXmlhttp;
8
if (this.readyState == 4 && this.status == 200) {
9
var allbook = JSON.parse(this.responseText);
10
for (var i = 0, len = allbook.Items.length; i < len; i++) {
11
id = allbook.Items[i].id
12
document.querySelector('.slideshow-container').innerHTML += `
13
<div class="mySlides fade">
14
<div class="numbertext">${i+1}/${allbook.Items.length}</div>
15
<img id="img" src onerror="this.onerror=null; this.src=myFunction(id);" style="width:100%">
16
<div class="text" id="title" onclick="myFunction(id)"></div>
17
</div>`;
18
document.querySelector('#punt').innerHTML += `
19
<span class="dot" onclick=currentSlide(${i+1})></span>`;
20
myFunction(id);
21
}
22
}
23
};
24
outerXmlhttp.open("GET", url, true);
25
outerXmlhttp.send();
26
27
showSlides(slideIndex);
28
29
function plusSlides(n) {
30
showSlides(slideIndex += n);
31
}
32
33
function currentSlide(n) {
34
showSlides(slideIndex = n);
35
}
36
37
function showSlides(n) {
38
var i;
39
var slides = document.getElementsByClassName("mySlides fade");
40
var dots = document.getElementsByClassName("dot");
41
if (n > slides.length) { slideIndex = 1 }
42
if (n < 1) { slideIndex = slides.length }
43
for (i = 0; i < slides.length; i++) {
44
slides[i].style.display = "none";
45
}
46
for (i = 0; i < dots.length; i++) {
47
dots[i].className = dots[i].className.replace(" active", "");
48
}
49
slides[slideIndex - 1].style.display = "block";
50
dots[slideIndex - 1].className += " active";
51
}
52
53
function myFunction(id) {
54
var url1 = "https://wjko8t4509.execute-api.us-east-2.amazonaws.com/books/" + id;
55
innerXmlhttp = new XMLHttpRequest();
56
innerXmlhttp.onreadystatechange = function () {
57
if (this.readyState == 4 && this.status == 200) {
58
var myArr = JSON.parse(this.responseText);
59
document.getElementById("img").src = "book_img/" + myArr.Item.immagine;
60
document.getElementById("title").innerHTML = `<a href="fragmentProva.html?id=${id}">${myArr.Item.titolo}</a>`;
61
}
62
};
63
innerXmlhttp.open("GET", url1, true);
64
innerXmlhttp.send();
65
}
JavaScript
1
9
1
<div class="slideshow-container" id="slideshow">
2
<a class="prev" onclick="plusSlides(-1)">❮</a>
3
<a class="next" onclick="plusSlides(1)">❯</a>
4
</div>
5
6
<br>
7
8
<div id="punt" style="text-align:center">
9
</div>
I would not like there to be some setup problem regarding the querySelector or the function call. I need your help, help me please!
Advertisement
Answer
showSlides
runs before the mySlides
container is populated from outerXmlhttp
try calling showSlides
from outerXmlhttp
JavaScript
1
3
1
showSlides(slideIndex);
2
myFunction(id);
3