What steps should I take to update the image src within my slider?

I have a slider image that I want to fade to the next image when a button is clicked. I used JQuery to create a smooth transition when changing the image source, but the image remains the same after fading.

Html

    <button onclick = "prev()" id = "prev">Prev</button>
    <img id = "slider" src = "" height = "600px" width = "600px"/>
    <button onclick = "next()" id = "next">Next</button>

JQuery

    var images = ['', '', ''];
    var num = 0;

    function next() {
        var slider = $('#slider');
        num++;
        if(num >= images.length) {
            num = 0;
        }
        slider.fadeOut('slow', function() {
            slider.src = images[num];
            slider.fadeIn('slow');
        });
    }

    function prev() {
        var slider = $('#slider');
        num--;
        if(num < 0) {
            num = images.length-1;
        }
        slider.fadeOut('slow', function() {
            slider.src = images[num];
            slider.fadeIn('slow');
        });
    }

I have removed the image links I used for testing as they were too lengthy. The three slider images would be stored in the "images" variable in JQuery and the initial one will be placed in the image src.

Answer №1

If images represents a collection of image links, the issue lies in your attempt to assign slider.src as opposed to slider.attr('src', value);.

For the corrected approach, implement the following:

slider.attr('src', images[index]);

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

Error in TypeScript while running the command "tsd install jquery" - the identifier "document" could not be found

Currently, I am facing an issue with importing jQuery into my TypeScript project. In order to achieve this, I executed the command tsd install jquery --save, which generated a jquery.d.ts file and added /// <reference path="jquery/jquery.d.ts" /> to ...

CSS sliding element moving horizontally

This code is currently set up to slide from right to left. #panel { position: absolute; right: 0; width: 180px; top: 0; bottom: 0; box-shadow: 0px 0px 15px black; background: white; padding: 10px; -webkit-transform-origin: 100% 50%; ...

Designing an HTML and CSS footer navigation bar

Can you help me create a footer menu using HTML and CSS? Check out this image for reference Any tips on how to style it with cascading style sheets? <footer> <ul> <li><a href="">Home</a> </li> <li> ...

Document ready not functioning properly for redirects

My code below seems to have an issue as it is not directing to the link specified. <html> <head> <script type="text/javascript" src="<?= baseurl(); ?>/public/js/jquery-1.9.1.min.js"></script> <script type=" ...

Is there a way to access the value variable from a JavaScript file located in the javascript folder and bring it to the routes/index.js file in a Node.js and Express application?

I'm currently working on transferring the input value from an HTML search box to the index route file in Node.js using Express. I have successfully retrieved the value from the search box in the javascript/javascript.js file, and now my objective is t ...

Google App Engine displaying a blank screen error

I've been playing around with this concept where there's a table of 'events' on a '/search' page. When the 'GO' button of an event is clicked, it should increase the 'RSVP' count for that event and redirect ...

Using the Ruby on Rails link_to tag along with a mouseenter jQuery function allows for dynamic

How can I make an image display in a div when the mouse hovers over a link_to using Rails? The code below seems to work, but I'm unsure of how it functions. <a href="#" class="nav-link roll"> <%= image_tag("chair1.jpg") %>Chesterfield Cha ...

Can anyone explain why the background images aren't appearing correctly on iPhones and iPads?

I've been attempting to resolve the issues on this page for what seems like forever. The site in question is theafropick.co.uk. All devices seem to function properly except for iPads and iPhones, where the section with 6 tiles disappears entirely. ...

Parsing JSON data in JavaScript to extract multiple records and store them in individual variables in memory

Although I have utilized JSON stringify and JSON Parse before, I am currently struggling to split up my JSON result into multiple variables. Challenge: My JSON result contains multiple sections or pages, and my objective is to store each of these results ...

File uploads in Django may pose a challenge when the request is sent from Ajax along with a form

I'm facing an issue while attempting to upload a file in Django using Ajax post. Unfortunately, it doesn't seem to work as expected. Here is the HTML code snippet responsible for handling the upload: <form action="{% url 'saveprof' ...

The JavaScript function may fail to work if the AJAX request is not completed

Upon completion of my ajax request, my function does not seem to be functioning as expected. Here is the script I am using: <script> function sendmsg(id,msg){ alert('id is'+id+'and msg is '+msg); } <scr ...

Guide to submitting a form with an image upon pressing a button

I am working with the following HTML structure: <form id="form" enctype="multipart/form-data"> <div class="row"> <div class="col"> <div class="mb-3"> ...

DropDownListFor with automatic postback feature

Is there a way to enable automatic postback for DropDownListFor in MVC? Currently, I have to manually refresh the page after selecting a value from the dropdown to see the changes reflected on the page. In View, The code for the dropdownlistfor looks lik ...

The Jasmine test does not instantiate a new class instance

I've been working on incorporating a Jasmine test into my JavaScript program (which is just a game), but I'm running into an issue where new instances aren't being created. I'm hoping that someone could help me troubleshoot and identify ...

Tips for ensuring a div stays centered while resizing the window

When I resize the window, the div tab on the right side of the screen moves to the bottom right. How can I make it stay in the middle regardless of screen size? I've tried using margin-left:auto and margin-right:auto but it didn't work. Changing ...

New project was successfully generated, but the information is missing when transmitted from React to Django API

I recently developed a React + Django application and implemented a basic CRUD feature. Everything was working smoothly until I encountered an issue while creating a project and storing it in the Django database. When I view the project at projects/list, o ...

Prevent unchecking a checked list item by clicking on it

Is it possible to prevent the user from unchecking a list item once it has been checked? Your assistance is greatly appreciated! $(".collectioncontainer ul li").click(function(){ $('.collectioncontainer ul li.checked').not(this).removeClass( ...

Sending data via AJAX to MVC 4 controller

Having trouble passing two values through AJAX to my SaveRating method in the ArticleController. The values being passed (artID, v) appear to be correct, but I keep receiving an error alert from AJAX and can't figure out why. //Edit:Possible Cause: ...

`"Type is invalid" error occurring with component after importing it into a different project``

I am currently working on developing a custom Storybook 7 Typescript component library with React. I have successfully imported this library into another project using a private NPM package. However, one of the components in the library, specifically the ...

Completing the regex properly

When using my editor, I am able to paste a video URL which is then converted by regex into an embed code. The URL in the WYSIWYG-editor looks like this: Once converted, the output HTML appears as: <p>http://emedia.is.ed.ac.uk:8080/JW/wsconfig.xml& ...