When you click on the Prev/Next arrow in the JS slider, the image suddenly

I've been struggling to find a solution for this issue. This is my first attempt at using JS as I am in the process of learning. It's very likely that I'm not fully grasping the answers from my searches - apologies for that.

What I'm looking for is a JS carousel, created from an array, complete with Prev/Next buttons (eventually making it responsive, but that can wait). My main goal is to have captions displayed below each image. I managed to get the carousel working, but when I click on Prev or Next, I end up getting a text link instead. And adding the caption array has proven to be quite challenging (I temporarily removed the JS for captions as it was causing further issues).

Here is the relevant HTML:

<body onload="changePilt()">
  <span id="prev" class="arrow">&#10094;</span>
    <div class="karussell" id="karussell">
      <a href="#"><img class="karu" name="esislaid"></a>
    </div>
  <span id="next" class="arrow">&#10095;</span>
  <div class="caption">
    <h3 name="esikiri"></h3>
  </div>
</body>

Also, here is the CSS code:

.karussell {
  position: relative;
  width: 100%;
  max-height: 600px;
  overflow: hidden;
}

.arrow {
  cursor: pointer;
  position: absolute;
  top: 40%;
  width: auto;
  color: #00A7E0;
  margin-top: -22px;
  padding: 16px;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}

#next {
  right: 0;
  border-radius: 3px 0 0 3px;
}
#prev {
  left: 0;
}
.arrow:hover {
  background-color: rgba(0,0,0,0.8);
}
.caption {
  text-align: center;
  color: #00A7E0;
  padding: 2px 16px;
}
.karu {
    max-width: 75%;
}
@media (max-width:767px){.karu{max-width: 95%;}}

And finally, the JavaScript section:

var i = 0;
var s = 0;
    var esileht = [];
    var aeg = 5000;

    //Image List
    esileht[0] = 'img/tooted/raamat/graafvanalinn2016.jpg';
    esileht[1] = 'img/tooted/kaart/kaart_taskus_esipool.jpg';
    esileht[2] = 'img/tooted/kaart/graafkaart_esikylg.jpg';

    //Change Image
    function changePilt (){
        document.esislaid.src = esileht[i];

        if(i < esileht.length -1){
        i++;
        } else {
        i = 0;
        }
        setTimeout("changePilt()", aeg);
    }


document.onload = function() {
}

// Left and Right arrows


    //Next
    function jargmine(){
        s = s + 1;
        s = s % esileht.length;
        return esileht [s];
    }
    //Previous
    function eelmine(){
        if (s === 0) {
            s = esileht.length;
        }
        s = s -1;
        return esileht[s];
    }

document.getElementById('prev').addEventListener('click', function (e){
    document.getElementById('karussell').innerHTML = eelmine();
}
);

document.getElementById('next').addEventListener('click', function (e) {
    document.getElementById('karussell').innerHTML = jargmine();
}
);

I believe the solution is straightforward, but unfortunately, I haven't been able to figure it out...

Answer №1

Replace the innerHTML with the src attribute of the image tag

document.querySelector('#karussell img').src = previous();

Also, update to:

document.querySelector('#karussell img').src = next();

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

What was the reason for the failure of my different sorting method?

I attempted to create a sorting algorithm without referring to any other code or resources. However, I am puzzled as to why my code doesn't produce any output. It seems to run for a while and then abruptly stop. I'm seeking guidance on where I m ...

I'm having trouble grasping the sequence of events in this async example

Currently, I'm delving into the world of JavaScript and Node.js, but I find myself facing challenges with asynchronous execution. I have a piece of code that may not be following best practices, but I am eager to understand why the file remains open w ...

Jquery: Undefined Key/Value Declaration

I'm diving into the world of associative arrays for the first time and trying to access data using keys. While my array is being constructed successfully, I'm struggling to actually retrieve the data. As a result, the console.log statement at the ...

In three.js, it is important to understand that Vertex and Vector3 are not simply

When I started using three.js revision 48, I created vertices connected by lines without any issues. However, upon updating to revision 65 from 48, an error message appeared stating that Vertix is deprecated and should be replaced by Vector3. Unfortunately ...

Generate a random weighted value from an array using PHP

I'm working with this script: $domain = ['gmail.com', 'yahoo.com', 'hotmail.com']; $domain = $domain[mt_rand(0, count($domain) - 1)]; Is there a way to assign different percentages to each item for the chances of being ...

Tips for preventing a bootstrap 5 button group from automatically centering after being clicked

I am facing an issue with a Bootstrap 5 button group inside a modal. The height is set to style="overflow-y: auto; max-height: 290px; min-height: 290px;" Whenever a button inside the modal div is clicked, the scroll centers to the middle button. I want to ...

Changing images dynamically in tinymce using JavaScript

When using the tinymce editor, I attempt to modify my images. I currently have an image within it and I am trying to dynamically change the path of this image with: tinymce.activeEditor.selection.getNode().src = '/my/path/' Surprisingly, this m ...

Pressing the Like Button triggers the transfer of a specific variable from PHP to jQuery

I have a piece of PHP code that I am using to display all users' posts, each with its own unique 'like' button: $sql = "SELECT * FROM posts"; $result = mysqli_query($con,$sql); while($row=mysqli_fetch_assoc($result)){ $post_content = $ro ...

What are the steps for integrating mongoDB with an angular2 application?

I currently have my angular2 & mongoDB setup successfully. While I've managed to read JSON files using the HTTP service, my goal is to create a fully functional application with database connectivity as well. I'm seeking advice on how to con ...

Error on Laravel 7: Unable to retrieve resource - the server returned a 400 (Bad Request) status code

Seeking assistance with a technical issue involving Razorpay. I'm encountering difficulty passing a value from the controller to JavaScript in the view. The data-order_id field is not populating, resulting in the error mentioned above, indicating a nu ...

What is the process for activating JavaScript and embedding it into an HTML document?

I am currently utilizing a JavaScript API that contains several functions. How can I incorporate it into an HTML file? One of the functions, "api.ping()", performs well on PowerShell, but I am encountering difficulties with displaying it on an HTML file. ...

Combine two elements in an array

I am faced with a challenge in binding values from an Array. My goal is to display two values in a row, then the next two values in the following row, and so on. Unfortunately, I have been unable to achieve this using *ngFor. Any assistance would be greatl ...

Using Next.js: What is the process for dynamically registering the quill-blot-formatter to react-quill that has been imported on the client side rendering exclusively

Currently, I am dynamically importing the react-quill library on the client side only by setting ssr: false. My functional component is functioning properly, but I now want to integrate the quill-blot-formatter package into the modules section of my quill ...

Retrieving the passed argument variable and utilizing it as a key in a map

I've been working on a JavaScript project recently and I've encountered a situation where I believe my code could be significantly reduced - up to 50% - if I could access the passed arguments to a function and use them as keys. Since I'm sti ...

Incorrect .offset().top calculation detected

Within a webpage, I have multiple sections. Positioned between the first and second section is a navbar menu. As the user scrolls down and the navbar reaches the top of the page, a function is triggered to fix it in place at the top. While this functionali ...

The appearance of HTML varies depending on the type of mobile device being used

My email template is having display variations on different mobile devices, with the textbox sometimes centered instead of left aligned as intended. Any suggestions on what might be causing this issue and how to resolve it? Desired Display on All Devices ...

Is there a delay in using ngShow and ngClick for authentication in AngularJS and Firebase?

Just getting started with AngularJS and encountering an unusual issue with Firebase authentication. The basic setup displays the current user status (logged in or not) along with options to sign in and out. Oddly, when I click the Log-in button for the fi ...

Animation with multiple meshes in Three.js

Having just started working with threejs, I decided to use a for loop to create 400 cylinders. The rendering of the objects in the scene works perfectly. However, when it comes to animating the cylinders, only one out of the 400 seems to rotate. How can I ...

When the value of a Formcontrol is changed using valueAccessor.writeValue(), it remains unchanged

Encountering a similar issue as seen in this stack overflow post, but the solution provided isn't resolving the issue. Perhaps you can offer assistance on that thread. In my scenario, I have created a directive for formatting phone numbers: import { ...

Encountered an error with Aurelia webpack 4 when trying to load a necessary CSS file during runtime

I encountered a unique issue with webpack and aurelia that I can't seem to figure out. After creating a new webpack configuration based on online resources and official documentation, the compilation goes smoothly without any errors. However, during r ...