In: Computer Science
What did I do wrong? When you click on the small caption of the photo, it is supposed to have a call to AJAX that changes the small description of my fav activities to a bigger description.
For example, when you click "cooking" it should change to what I have in the biginfo file but it does not change.
This is the full code. The "biginfo" files I have are done, don't have anything but the fuller description.
HTML
_________________
<!DOCTYPE html>
<html>
<head>
<title>Slideshow</title>
<meta name="viewport" content="width=device-width,
initial-scale=1">
<meta content="text/html; charset=iso-8859-2"
http-equiv="Content-Type">
<link rel="stylesheet"
href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.mySlides {display:none;}
.onPic{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: black;
font-size: 12px;
}
</style>
</head>
<body>
<h2 class="w3-center">My Favorite Actitivies</h2>
<div class="w3-content w3-section"
style="max-width:500px">
<div class="mySlides">
<img src="cooking.jpg" style="width:100%">
<button class="onPic" type="button"
onclick="loadDoc1()">Cooking</button>
</div>
<div class="mySlides">
<img src="lacrosse.jpg" style="width:100%">
<button class="onPic" type="button"
onclick="loadDoc2()">Lacrosse</button>
</div>
<div class="mySlides">
<img src="jogging.jpg" style="width:100%">
<button class="onPic" type="button"
onclick="loadDoc3()">Jogging</button>
</div>
<div class="mySlides">
<img src="music.jpg" style="width:100%">
<button class="onPic" type="button"
onclick="loadDoc4()">Listening to music</button>
</div>
<div class="mySlides">
<img src="dog.jpg" style="width:100%">
<button class="onPic" type="button"
onclick="loadDoc3()">Playing with my dog</button>
</div>
<div class="mySlides">
<img src="tv.jpg" style="width:100%">
<button class="onPic" type="button"
onclick="loadDoc4()">Watching TV</button>
</div>
</div>
<script>
var myIndex = 0;
carousel();
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 7000); // Change image every 2 seconds
}
function loadDoc1() {
var xhttp1 = new XMLHttpRequest();
xhttp1.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp1.open("GET", "biginfo1.txt", true);
xhttp1.send();
}
function loadDoc2() {
var xhttp2 = new XMLHttpRequest();
xhttp2.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp2.open("GET", "biginfo2.txt", true);
xhttp2.send();
}
function loadDoc3() {
var xhttp3 = new XMLHttpRequest();
xhttp3.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp3.open("GET", "biginfo3.txt", true);
xhttp3.send();
}
function loadDoc4() {
var xhttp3 = new XMLHttpRequest();
xhttp3.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp3.open("GET", "biginfo4.txt", true);
xhttp3.send();
}
function loadDoc5() {
var xhttp3 = new XMLHttpRequest();
xhttp3.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp3.open("GET", "biginfo5.txt", true);
xhttp3.send();
}
function loadDoc6() {
var xhttp3 = new XMLHttpRequest();
xhttp3.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp3.open("GET", "biginfo6.txt", true);
xhttp3.send();
}
</script>
</body>
</html>
BIGINFO1
--------------------
"I like to cook because it is fun"
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
NOTE :
Here a new web page with name "ajax1.html" is created, which contains following code.
ajax1.html :
<!DOCTYPE html>
<html>
<head>
<!-- title for web page -->
<title>Slideshow</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="text/html; charset=iso-8859-2" http-equiv="Content-Type">
<!-- link is used to refer to external stylesheet -->
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<!-- internal style -->
<style>
.mySlides {display:none;}
.onPic{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: black;
font-size: 12px;
}
</style>
</head>
<body>
<h2 class="w3-center">My Favorite Actitivies</h2>
<div class="w3-content w3-section" style="max-width:500px">
<div class="mySlides">
<img src="cooking.jpg" style="width:100%">
<button class="onPic" type="button" onclick="loadDoc1()">Cooking</button>
</div>
<div class="mySlides">
<img src="lacrosse.jpg" style="width:100%">
<button class="onPic" type="button" onclick="loadDoc2()">Lacrosse</button>
</div>
<div class="mySlides">
<img src="jogging.jpg" style="width:100%">
<button class="onPic" type="button" onclick="loadDoc3()">Jogging</button>
</div>
<div class="mySlides">
<img src="music.jpg" style="width:100%">
<button class="onPic" type="button" onclick="loadDoc4()">Listening to music</button>
</div>
<div class="mySlides">
<img src="dog.jpg" style="width:100%">
<button class="onPic" type="button" onclick="loadDoc3()">Playing with my dog</button>
</div>
<div class="mySlides">
<img src="tv.jpg" style="width:100%">
<button class="onPic" type="button" onclick="loadDoc4()">Watching TV</button>
</div>
<div id="demo"></div>
</div>
<script>
var myIndex = 0;
carousel();
function carousel() {
var i;
var x = document.getElementsByClassName("mySlides");
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
myIndex++;
if (myIndex > x.length) {myIndex = 1}
x[myIndex-1].style.display = "block";
setTimeout(carousel, 7000); // Change image every 2 seconds
}
function loadDoc1() {
var xhttp1 = new XMLHttpRequest();
xhttp1.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp1.open("GET", "biginfo1.txt", true);
xhttp1.send();
}
function loadDoc2() {
var xhttp2 = new XMLHttpRequest();
xhttp2.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp2.open("GET", "biginfo2.txt", true);
xhttp2.send();
}
function loadDoc3() {
var xhttp3 = new XMLHttpRequest();
xhttp3.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp3.open("GET", "biginfo3.txt", true);
xhttp3.send();
}
function loadDoc4() {
var xhttp3 = new XMLHttpRequest();
xhttp3.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp3.open("GET", "biginfo4.txt", true);
xhttp3.send();
}
function loadDoc5() {
var xhttp3 = new XMLHttpRequest();
xhttp3.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp3.open("GET", "biginfo5.txt", true);
xhttp3.send();
}
function loadDoc6() {
var xhttp3 = new XMLHttpRequest();
xhttp3.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp3.open("GET", "biginfo6.txt", true);
xhttp3.send();
}
</script>
</body>
</html>
======================================================
Output : Open web page ajax1.html in the browser and will get the screen as shown below
Screen 1 :ajax.html
Screen 2:Screen when clicked on button
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.