Display the following image from a div upon clicking the button

In short, I am using WP Google Map Pro to display posts which does not support shortcodes for galleries. As a result, I had to create my own gallery with images as shown below:

<div id="post_content">
<img src="image1.jpg" class="thumb" />
<img src="image2.jpg" class="thumb" />
<img src="image3.jpg" class="thumb" />
</div>

<div class="showimagediv"></div>

This is done using jquery and it works well:

<script>
$(document).ready(function() {
    $('.thumb').on('click', function() {
        var img = $('<img />', {src    : this.src,
                                'class': 'fullImage'
                  });

        $('.showimagediv').html(img).show();
    });

        $('.showimagediv').on('click', function() {
        $(this).hide();
    });
});
</script>

However, I am looking to enhance the gallery to include lightbox functionality with previous and next arrows.

My main question is: How can I achieve this? Is there a way to load the next image from the same div into "showimagediv" and replace image1 with image2? Any suggestions?

Answer №1

I believe this is what you are searching for:

$(document).ready(function() {

    $(".showimagediv").html($("img").eq(0).clone())
    var pos = 0;
    $('#prev').on('click', function() {
        if(pos>0) pos--
          $(".showimagediv").hide().html($("img").eq(pos).clone()).fadeIn("slow");
    });

    $('#next').on('click', function() {
        if(pos<2) pos++
        $(".showimagediv").hide().html($("img").eq(pos).clone()).fadeIn("slow");
    });
    
    $('.showimagediv').on('click', function() {
        $(this).hide();
    });
});
.img1{
 width: 50px;
 height: 50px;
}
.img2{
 width: 70px;
 height: 70px;
}
.img3{
 width: 90px;
 height: 90px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="post_content">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66" class="thumb img1" />
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66" class="thumb img2" />
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66" class="thumb img3" />
</div>
<button id="prev">Prev</button>
<button id="next">Next</button><br><br>
<span></span>
<div class="showimagediv"></div>

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 is the best way to make content stretch to 100% width when there is no content in the sidebar?

As I work on developing my custom theme for Drupal, I am seeking a solution that will allow the content width to adjust depending on the presence of a sidebar. When there is no sidebar, I want the content width to be 100%, and when a sidebar is present, I ...

Creating a dynamically sorting page for displaying products by utilizing a combination of PHP, AJAX, jQuery, and a dropdown menu

I'm currently working on implementing a dropdown menu that allows users to sort a page by "price high-low" or "price low-high". Below is the HTML code with an ID of "show_product": <div class="row" id ="notification-bar"></div> <div c ...

Summer Reminder (What You See Is What You Get) - Problem with uploading images as files

In my Python Flask application, I am utilizing version 0.8.18 of summer note. I have integrated the following libraries: <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iE ...

The concatenation of Ajax results appears to append to the existing data

My ajax function handles uploading comments to a form, which then returns the same string. If successful, the comment is added to a comment box and the input text is cleared. The issue arises when a user adds another comment; the new comment is appended w ...

Changing the background color of the canvas using Javascript

I want to create a rect on a canvas with a slightly transparent background, but I don't want the drawn rect to have any background. Here is an example of what I'm looking for: https://i.stack.imgur.com/axtcE.png The code I am using is as follo ...

What is the purpose of the code snippet 'jQuery.each(lines, function(lineNo, line)'?

As a newbie to jquery and ajax, I stumbled upon some code online that caught my attention. The code snippet below prompted me to question its purpose and functionality: lines = newLine.split('#'); jQuery.each(lines, function(lineNo, line) ...

jQuery form validation ceases to function following the implementation of a datepicker

I am in the process of creating a booking (reservation) form that includes a datepicker textfield, name, contact information, and more. The validation is working perfectly for all inputs except the datepicker field. Once I added the jQuery datepicker, th ...

What could be causing my straightforward Ajax request to not function as desired?

My homepage features a simple design that includes the following code: <h2>---Select your RuleType---</h2> @using (Ajax.BeginForm("LoadRules", "Home", new AjaxOptions { UpdateTargetId = "Rules" })) { @Html.DropDownListFor(x => x._Sele ...

Exploring the benefits of the AjaxHelper class in conjunction with jQuery within asp.net mvc applications

I've incorporated jQuery and jQuery UI throughout my project using HtmlHelper extensions, but for some reason I haven't fully grasped the benefits of utilizing the AjaxHelper. (or maybe I'm just unaware) I'm intrigued to learn about the ...

What is the best way to integrate a button within a datatable cell to display additional details in a popup window?

I am seeking help with datatable.js. I would like to insert a button inside the cells of the datatable columns to expand and display the detailed content of each cell. When I click on the red button, a popup modal should appear showing the full list of con ...

Is there a way to effectively test the jQuery animate functions such as fadeTo and fadeOut using js-test-driver?

Trying to create test cases for my JavaScript functions has become frustrating due to a specific issue I encountered. In my script, there is a section of code similar to the following: $("idOfTheDiv").fadeOut('fast'); or: $("idOfTheDiv").fade ...

Reveal the table only once a search has been completed

Currently, I am in the process of developing a small project and my aim is to have the table with the results appear only upon clicking the button or initiating a search. If you are interested, you can view it at this link: . My objective is to keep the t ...

Guide on adding a class to the `li` element depending on the content within with the use of jQuery

var id is a dynamic value that changes. How can I dynamically add the class='current' to the list item with the same text as the ID? For example, when id=1, <li class="current"><a href="http://localhost/myweb/index.php/blog/index">1& ...

Guide to setting up a nested vuetify navigation drawer

I am facing a scenario where I have one fixed navigation drawer with icons and another dynamic navigation drawer that should open and close adjacent to the fixed one without overlapping. The current implementation is causing the dynamic drawer to overlap t ...

show button after the page has finished loading

I have a button similar to this: <input type="submit" id="product_197_submit_button" class="wpsc_buy_button" name="Buy" value="Add To Cart"> However, I am encountering an issue where if the user clicks the button before all scripts are loaded, an e ...

What is the best way to transfer a segment of CSS, HTML, and JavaScript code from one static template to another?

I have a collection of static files (html, css, and js) and I've identified a specific piece of code (let's say a dinosaur animation) that I want to move to another set of static files (a separate project I'm working on with a different temp ...

Disabling components within a checkbox group

In Wordpress, I am attempting to target a checkbox group using HTML Snippets. My goal is to grey out checkboxes within the group when a session (represented by one of the checkboxes) reaches maximum capacity. Despite trying various combinations like this: ...

What are the steps to create a responsive Coin Slider?

Once the slider is generated, it appears that there is no built-in method to resize it, causing issues with responsive design. Is there a way to adjust the size of the Coin Slider plugin based on the media queries in Twitter Bootstrap 3? Take a look at C ...

Implement a vertical scrollbar within the Bootstrap navbar to accommodate a large number of items

I implemented a sidebar using Bootstrap within a React application. Below is the code snippet: import React from 'react'; import './style_sidebar.css'; export default class Sidebar2 extends React.Component { render() { ret ...

jQuery autocomplete feature is not functioning properly on dynamically generated input fields

I am encountering an issue with autocomplete on dynamically created fields. In the image attached, I have created dynamic fields by clicking on the plus sign. Autocomplete is working on the first row, but for the rest of the dynamically generated rows, au ...