Learn how to update images by importing them on a button click!

In the homepage, there are 3 images and 2 buttons - next and previous. When the next button is clicked, the display changes to the next set of 3 images from a total of 100 images. Clicking on next again will show the subsequent 3 images, and on the third click, only the last image will be displayed. The same concept applies when clicking on the previous button. Below is an incomplete code snippet for reference:

<html>
<head>
    <title>
        Check! 1
    </title>
    <style type="text/css"> 
        #myimage {
    position:absolute;
    left:800;
    top:500;
  height: 50px;
  width: 50px;
}
#myimage1 {
    position:absolute;
    left:500;
    top:500;
 height: 50px;
width: 50px;
}   
#imageDiv
{
position:static;
top: 50px;
left: 10px;


}
    </style>
    <script type="text/javascript">
    var count=0;    
function image(thisImg) {
var img = document.createElement("IMG");
img.src = "img/"+thisImg;
document.getElementById('imageDiv').appendChild(img);
}
function test()
    { 

        //alert("just checking yaar");
        if(count<4)
        {
        ++count;
        console.log("count",count);
        if(count==1)
        {
            image('44585_Murree-Best-Hill-Station-wallpapers-        pictures_640x320.jpg');
            image('91517_background-painting-used-Main-Menu-screen_640x320.jpg');
            image('gravityflue04-640x320.jpg');
        }
        else if(count==2)
        {
            image('44585_Murree-Best-Hill-Station-wallpapers-pictures_640x320.jpg');
            image('91517_background-painting-used-Main-Menu-screen_640x320.jpg');
            image('gravityflue04-640x320.jpg');

        }
        else if(count==3)
        {
            image('44585_Murree-Best-Hill-Station-wallpapers-pictures_640x320.jpg');
            image('91517_background-painting-used-Main-Menu-screen_640x320.jpg');
            image('gravityflue04-640x320.jpg');
        }
        else if(count==4)
        {

            image('44585_Murree-Best-Hill-Station-wallpapers-pictures_640x320.jpg');
            image('91517_background-painting-used-Main-Menu-screen_640x320.jpg');
            image('gravityflue04-640x320.jpg');
        }
        else 
        { 
            console.log("Invalid Count");
        }
    }
    }
    function test2()
    {

        if(count>0)
        {
        --count;    
        console.log("count",count);
        }
        else
        {
            console.log("Invalid Count");
        }
    }

</script>
</head>
<body>
<input type="image" id="myimage" value="next" name="next" src="next-button.jpg"   onclick="test()">
    <input type="image" id="myimage1" value="" name="next"  src="111645-glowing-green-neon-icon-media-a-media31-back.png" onclick="test2()">

Answer №1

Populate the array with the names of the photos. The number of <img /> tags left empty (within the photo <div />) determines how many images will be shown at one time. Update the displayImage function to reference the directory containing the photos.

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <button onclick="previous();">Previous</button>
    <div id="images">
        <img />
        <img />
        <img />
    </div>
    <button onclick="next();">Next</button>
    <script>
        var photoSources = Array(
            "photo1.png", "photo2.png", "photo3.png",
            "photo4.png", "photo5.png", "photo6.png",
            "photo7.png", "photo8.png", "photo9.png",
            "photo10.png");
        var firstPhoto = 0;
        var increment = document.getElementById("images").children.length;
        displayPhotos();
        function next() {
            if (firstPhoto + increment < photoSources.length) {
                firstPhoto += increment;
            }
            displayPhotos();
        }
        function previous() {
            if (firstPhoto - increment >= 0) {
                firstPhoto -= increment;
            }
            displayPhotos();
        }
        function displayPhotos() {
            var photoDiv = document.getElementById("images");
            for (var photoIndex = 0; photoIndex < photoDiv.children.length ; photoIndex++) {
                displayImage(photoDiv.children[photoIndex], photoSources[firstPhoto + photoIndex])
            }
        }
        function displayImage(photo, src) {
            if (src) {
                photo.src = "photos/" + src;
                photo.style.display = "";
            } else {
                photo.style.display = "none";
            }
        }
    </script>
</body>
</html>

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

Utilizing ElementRef in Angular 4 to close dropdown when clicking outside of it

I recently came across this helpful tutorial, but I'm having trouble grasping how it actually functions. Here's the code snippet I've incorporated into my TypeScript file: @Component({ host: { '(document:click)': 'onOuts ...

Is it possible for nextAll() to exclude a specific parent element and search only for children inside that parent?

Below is the structure of my html ajax form: <form name="vafForm" id="vafForm" method="GET" action="urlfor ajax"> <input type="hidden" value="0" id="vafProduct"> <select class="catSelect {prevLevelsIncluding: 'cat'} c ...

Change Class Depending on Vertical Scroll Position

Take a look at the Fiddle provided below: https://jsfiddle.net/y0mj6v2d/5/ I'm currently trying to figure out the most effective approach for determining when to apply or remove a class based on the vertical scroll position. My goal is to incorpora ...

Activate only one option group at a time

<select name="location"> <optgroup label="West Coast"> <option value="1">Los Angeles</option> <option value="2">San Francisco</option> <option value="3">Seattle</option> &l ...

What is the best approach for connecting Advanced Custom Fields in WordPress to components in my Next.js frontend using GraphQL?

Currently, I am in the process of constructing a website by utilizing WordPress as the headless CMS and NextJs for my frontend. My dilemma lies in using the free version of ACF and wanting to dynamically query and map ACF to components in Next when generat ...

The Sequelize error message states: TypeError: an array or iterable object was expected, but instead [object Null] was received

I am encountering an issue with the findOne method from sequelize in my model. The error message I am getting states that the table referenced by the model is empty. How can I resolve this? Unhandled rejection TypeError: expecting an array or an iterable ...

picking out a particular set of data from a JSON document

I have a map of Europe along with a JSON file that displays the unemployment rate for each country in the year 2011. The JSON file also includes x and y elements, allowing me to place a blue circle on top of each country on the map. My goal is to be able ...

Changing data in vue

I am utilizing a mixin in my Vue code to both add and override data properties. While I can successfully add new data, I am facing issues when trying to override existing data values. Below is a snippet of the code: var mixin = { data: function() { ...

Tips on personalizing the FirebaseUI- Web theme

Can someone help me find a way to customize the logo and colors in this code snippet? I've only come across solutions for Android so far. if (process.browser) { const firebaseui = require('firebaseui') console.log(firebaseui) ...

What is preventing me from retrieving the coordinates from the marker in the Google Maps Places API?

Is there a way to obtain the latitude and longitude from a marker in google maps when it is relocated to another position? I have researched and located the solution, however, I seem to be encountering an issue. While I can successfully retrieve the coord ...

The download of package-lock.json is not initiated for a linked GitHub URL

I currently have two projects on GitHub. One is named "mylibrary" and the other is "test-project." In my "test-project," I have linked "mylibrary" using its GitHub URL in the package.json file as shown below. dependencies: { "mylibrary": "git+ssh://& ...

Occasional issue with the drop down menu not appearing correctly

I'm experiencing some difficulties with displaying a dropdown menu. Check out the fiddler link for reference: http://jsfiddle.net/GxrSk/ Below is the simplified HTML code: <nav> <ul id="top-menu"> <li class="parent"> ...

Can JavaScript be adapted to simulate the features of an object-oriented programming language?

Is there a method in Javascript to imitate an object-oriented language? For example, is it possible to replicate the ability to define custom classes/objects with properties? Given that JSON serves as a means for passing objects in JavaScript, I assume the ...

To retrieve the first td element by clicking a button within the corresponding row using JQuery

This may seem like a straightforward inquiry, but despite my best efforts, I have been unable to find a solution. I am looking to create a function that will allow me to click a button located in the 3rd td element and display the text from the first td e ...

The value of req.headers('Authorization') has not been defined

I'm experiencing difficulty with my code as the token is coming back as undefined. Here is the frontend section: export const fetchUser = async (token: any) => { const res = await axios.post('/user/getuser', { headers ...

Fixing PNG Fading Issue in Internet Explorer 8

I am currently utilizing the jQuery Cycle plugin to produce a fading animation effect. While this works perfectly on most browsers, I have encountered an issue with PNG files containing semi-transparent pixels in Internet Explorer 8. These partially transp ...

React components can be used to dynamically render and display an array of objects through methods like reduce and

Here's the scenario at hand: (https://codesandbox.io/s/8p21n6p09l) I have an array of objects (referred to as modules) structured like this: const modules = [ { thematicArea: "Topic 1", id: 1, name: "Building assertive attitude", d ...

Enhancing Filtering Capabilities with Multiple ng-Model in AngularJS

I am facing an issue with filtering a form based on user input in a text box or selection from a dropdown list. The text box filter works fine individually, but when I try to combine it with the dropdown list selection, neither filter seems to work. Below ...

What steps should I take to ensure that the JSON data is exclusively accessible to my JavaScript code?

Creating a web application that requires visualizing a significant amount of data using Charts. Discovered some interesting javascript libraries [dynagraph] that can handle this task. However, encountering an issue with using javascript to access data in J ...

What is the best way to retrieve a key from an Any type in Angular?

Currently, I am facing a challenge in extracting key values from an Any type while working with Angular/Typescript. For instance, if another segment of the program is providing this data: { Amy: { age: 7, grade: 2 }, Max: { ...