Tips for resolving image path issues in slider functionality

My banner sliding function isn't working and the image paths are also not being recognized

var img = document.getElementById('img-banner');

var images = [
    '../banners/banner.png', 
    '../banners/banner1.png',
    '../banners/banner2.png'
];

var x = 0;

function slide() {

    if (x > images.length) {
        x++
    } else {
        x = 1;
    }

    img.innerHTML = "<img src ="+images[x-1]+">";
}

setInterval(slide, 5000);

Answer №1

It is important to note that your x variable will always be equal to 1 because the code will never enter the if statement. The condition x > images.length will never evaluate to true.

You may want to consider using if( x < images.length ) instead.

Additionally, in your image source path, you are decrementing by 1 which will always display the zero index. There seems to be no issue with your path.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the capabilities of the Angular 2 expression parser alongside the functionality of the

Is there a way to create an equivalent of the Angular 1.x ngInit directive in Angular 2? I am familiar with the ngOnInit hook, which is recommended for initialization code. The ngInit directive seems like a quick and declarative way to prototype or fix a ...

What is the best way to show a dropdown menu with options based on the values stored in the useState hook in React and Material UI?

An API that I'm using fetches celebrity names. Currently, I am initializing the state and storing dummy data in it like this: const[drop,setDrop] = useState(["Virat Kohli","Selena Gomez","Deepika Padukone"]); Now, I want my drop-down to include the f ...

Using React.js to compute dates based on user-inputted dates

Today was dedicated to tackling the coding challenge of creating a function. I'm currently working on a react app using the PERN stack. The form I'm working on includes multiple date inputs, with two date columns and a total days column. My goal ...

Learn how to incorporate an input field into a form using the same class name

I am facing a challenging JavaScript issue that I have been struggling to resolve. My predicament involves a dynamically formed table with form fields. Here is the code snippet in question: var table = document.getElementById("table"); var row = table. ...

Guide to centering a Material UI Table on a webpage

Is it possible to center the <Table> within a fixed width <div>, and have a scrollbar appear when the browser is resized, while maintaining the fixed width of the <Table>? Thanks in advance. This is my current setup: <div> ...

Struggling to incorporate typeWriter.js into the code with no luck

I'm attempting to make this script work. The script is designed to be referenced from an external script file, but I need it to be embedded for certain reasons which I won't delve into. I believe I've set it up correctly, but it seems that w ...

Implementing expiration dates or future dates with jQuery

How can I modify this jQuery code to display an expiration date specified in months or years? For instance, I have created a Membership Card for a client that is valid for 2 years, and I would like to include an expiration date in the script. Thank you j ...

What are the steps for integrating Landbot into a Next.js application?

I've been attempting to integrate Landbot into my Next.js application, but I'm facing some difficulties. I tried to modify the _document.js file and insert the necessary code into the body section, however, it doesn't seem to have any impact ...

How can I send two responses in a single POST request using node.js?

Below is my router setup for handling responses: questionRouter.post('/questionsReply', (req, res) => { twilioResp(req, res); var newResponse = new Response(req.body); newResponse.save((err, data) => { if (err) return handleDBError(er ...

Why React's setState is only returning the last item when a list is saved to a variable

Being a newcomer to React, I am currently working on a small project that involves using the GitHub API to perform a search and display the results on the screen via AJAX. Right now, I am utilizing a for loop to iterate through the response data, saving it ...

Utilizing jQuery and Ajax to transmit an input array

I'm encountering an issue trying to send a form field array through my form, but unfortunately, I have not been successful. ...

Combining strings with JQuery

Looking for help with a code snippet <script> function goToDetails($i){ $(function(){ var transValue = $('#trans'+$i).html(); var mileageValue = $('#mileage'+$i).html(); var engineValue = $('#eng'+$i).html ...

regular expression for replacing only alphanumeric strings

I'm currently developing a tool that tags alphanumeric words based on the option selected from the right-click context menu. However, I am facing issues when a group of words containing special characters is selected. I have been using the following ...

Ways to choose the correct css styling

I'm currently working on creating navigation using Bootstrap 4, but I've hit a roadblock in adjusting the li items to suit my navigation needs. I'm struggling to figure out how to select these li tags since they are nested in multiple classe ...

How can I implement Jquery ajax calls in various JavaScript functions?

I'm encountering an issue with a particular function in my code function readComm(){ $.post("something.php", {read:"read"}, function(data){ retVal = $.trim(data).toString(); console.log(retVal); return retVal; }); } T ...

HTML: Determine the Precise Location of a Div Element That is Centered

Imagine I have this image: Is there a way for me to determine the x/y or left/top properties of my canvas if it is centered using the following CSS: #canvas-container { width: 100px; height:100px; margin: 0px auto; } Note ...

How can I trigger an audio element to play using onKeyPress and onClick in ReactJS?

While attempting to construct the Drum Machine project for freeCodeCamp, I encountered a perplexing issue involving the audio element. Despite my code being error-free, the audio fails to play when I click on the div with the class "drum-pad." Even though ...

DNN Unveils New "Exit Confirmation" Pop-up Feature When Clicking External Links

Greetings fellow beginners! I've been struggling to make some changes on our DNN site (Evoq 8.5) with no success so far. The issue at hand is that we have links throughout our entire website that follow this format: <a href="www.site.com" class="e ...

Retrieve only the top 10 results from a Redis database using Node.js

One of my tasks involves fetching a list of values from Redis and then transmitting it in JSON format to the client: members.hgetall(All,_) Just a heads up, I'm utilizing streamline.js for this project. I think having a feature similar to MySQL&a ...

Steps for choosing the nth HTML row with jQuery

I'm facing a situation where I need to be able to select the nth row of an HTML table based solely on the id of the selected row. You can see exactly what I mean by checking out this JSFiddle Demo <table class="mytable1"> <tr><td i ...