I am a true rookie in JS, I have this code, in the body of my HTML:
var secDuration = 5;
var image = 1;
var maxImages = 4;
var slider = document.getElementById('slider');
function changeImage(requiredImage) {
if (!requiredImage && requiredImage != 0){
if(image < maxImages){
image++;
}
else{
image = 1;
}
}
else{
if(requiredImage > maxImages){
image = 1;
}
else if(requiredImage < 1){
image = maxImages;
}
else{
image = requiredImage;
}
}
slider.className = "image"+image;
clearTimeout(timeout)
}
function nextImage(){
changeImage(image+1);
}
function prevImage(){
changeImage(image-1);
}
changeImage(1);
The trouble is that when i paste it in a js file which i properly connect to my HTML, the script does not work anymore.
I tried several solutions, but i’m propbably missing something. Help would be gladly appreciated.
Thanks in advance.
The valid embedding of JavaScript file is:
Make sure that your have done in the same way.
Then, you have to work with elements from your HTML page when your page is fully loaded, so move statements like
document.getElementByIdinwindow.onloadmethod:So, your updated script should look like this: