Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:22:54+00:00 2026-06-03T05:22:54+00:00

I am a true rookie in JS, I have this code, in the body

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:22:55+00:00Added an answer on June 3, 2026 at 5:22 am

    The valid embedding of JavaScript file is:

    <html>
    <head>
        ...
        <script type="text/javascript" src="/path/to/script.js"></script>
    </head>
    <body>
        ...
    </body>
    </html>
    

    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.getElementById in window.onload method:

    window.onload = function() {
        slider = document.getElementById('slider');
    };
    

    So, your updated script should look like this:

    var image = 1;
    var maxImages = 4;
    var 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);
    }
    
    window.onload = function() {
        slider = document.getElementById('slider');
        changeImage(1);
    };​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I have a true colored image, by using dithering algorithm, I can reduce
This code return true in Firefox: ('overrideMimeType') in (new XMLHttpRequest()) but jslint gives me
Assert.True(1==1, blah); When this code runs, blah does not appear in the NUnit GUI.
if(true) { string var = VAR; } string var = New VAR!; This will
Is this true? When you instantiate a UIAlertButton, you have to pass it an
if true && (function () { console.log('executed'); })() is ok, then why true &&
Is it true that the only way to handle default function arguments is through
I want a true deep copy. In Java, this was easy, but how do
Why next code returns true (Saxon-EE 9.2 for .NET)? matches('some text>', '^[\w ]{3,200}$') There
I'm sure I'm making a rookie mistake with java(this is actually my first program).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.