Troubleshooting problem with nested image carousel in Bootstrap modal

I am working on implementing a modal for displaying an image gallery. The initial modal showcases thumbnails, and upon clicking a thumbnail, a secondary modal should open on top with larger images displayed in a carousel. The issue I'm facing is that when I click on a thumbnail, the second modal appears but shows both thumbnails instead of the corresponding large image.

Although the code seems correct upon inspection, there seems to be a glitch preventing it from functioning as intended.

Below is the HTML structure:

<!-- kitchens modal -->
<div class="modal kitchens-modal fade" id="kitchens-modal" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <!-- modal header -->
              .....

The relevant CSS styles are:

.thumbnail {
    height: 100px;
    margin: 6px;
}

.thumbnail {
    margin-bottom: 6px;
}


.carousel-control.left,
.carousel-control.right {
    background-image: none;
    margin-top: 10%;
    width: 5%;
}

Finally, here is the associated JS code:

$('.row .thumbnail').on('load', function() {

}).each(function(i) {
  if(this.complete) {
        var item = $('<div class="item"></div>');
        var itemDiv = $(this).parents('div');
          .....

Despite having made modifications based on an online modal gallery, the functionality remains flawed. Upon clicking a thumbnail in the first modal, the second modal fails to display the corresponding large image and instead exhibits all thumbnails. Any suggestions or insights would be greatly appreciated!

Answer №1

Here is a possible solution:

$('.kitchens-modal .thumbnail').each(function(i) {
  var item = $('<div class="item"></div>');
  var itemDiv = $(this).parents('div');
  var title = $(this).parent('a').attr("title");

  item.attr("title", title);
  $(itemDiv.html()).appendTo(item);
  item.appendTo('.carousel-inner'); 
  if (i === 0) { // set first item as active
      item.addClass('active');
  }
});

/* activate the carousel */
$('#modalCarousel').carousel({interval: false});

/* update modal title when slide changes */
$('#modalCarousel').on('slid.bs.carousel', function () {
    $('.modal-title').html($(this).find('.active').attr("title"));
});

/* when a thumbnail is clicked */
$('.row .thumbnail').click(function(){
    var idx = $(this).parents('div').index();
    var id = parseInt(idx);
    $('#myModal').modal('show'); // show the modal
    $('#modalCarousel').carousel(id); // move carousel to selected item
});
.thumbnail {
    height: 100px;
    margin: 6px;
}

.thumbnail {
    margin-bottom: 6px;
}


.carousel-control.left {
    background-image: none;
    position: absolute;
    top: 50%;
    left: 0;
    width: 25px;
    height: 100%;
    transform: translateY(-50%);
}

.carousel-control.right {
    background-image: none;
    position: absolute;
    top: 50%;
    right: 0;
    width: 25px;
    height: 100%;
    transform: translateY(-50%);
}

.item .thumbnail {
  margin: 0 auto;
}

body {
  margin: 20px !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="btn btn-primary" data-target="#kitchens-modal" data-toggle="modal">Open Kitchen Modal</div>

<!-- kitchens modal -->
<div class="modal kitchens-modal fade" id="kitchens-modal" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <!-- modal header -->
            <div class="modal-header">
                <h1>KITCHENS</h1>
                <h3>Your dream kitchen is so close...</h3>
            </div>
            <!-- end of modal header -->

            <!-- modal body -->
            <div class="modal-body">
                <div class="row">
                    <div class="col-lg-2 col-sm-3 col-xs-6">
                        <a href="#">
                            <img class="thumbnail img-responsive" src="http://placehold.it/200x200" alt="Kitchen" title="Kitchen">
                        </a>
                    </div>
                    <div class="col-lg-2 col-sm-3 col-xs-6">
                        <a href="#">
                            <img class="thumbnail img-responsive" src="http://placehold.it/300x300" alt="Kitchen two" title="Kitchen two">
                        </a>
                    </div>
                </div>
            </div>
            <!-- end of modal body -->

            <!-- modal footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
            <!--end of modal footer -->
        </div>
    </div>
</div>
<!-- end of kitchens modal -->

<!-- inner kitchen gallery modal -->
<div class="modal" id="myModal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h3 class="modal-title"></h3>
            </div>

            <div class="modal-body">
                <div id="modalCarousel" class="carousel">
                    <div class="carousel-inner">

                    </div>

                    <a class="carousel-control left" href="#modalCarousel" data-slide="prev">
                        <i class="glyphicon glyphicon-chevron-left"></i>
                    </a>
                    <a class="carousel-control right" href="#modalCarousel" data-slide="next">
                        <i class="glyphicon glyphicon-chevron-right"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- end of inner kitchen gallery modal -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

I hope this information proves useful!

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

Separating a picture into distinct sections

Is there a way to divide an image into different parts to create hover effects? For example, hovering over the top part of the image triggers one change, and hovering over the bottom part triggers another change. This example demonstrates this concept usin ...

How to position a full-width banner image in the center using CSS without causing scrollbars

I am facing a design challenge with my webpage where the content is 1000px wide. In the middle of this page, I want to showcase an image that is 600px high and 2000px wide. The tricky part is ensuring that this image remains 600px high, maintains its aspe ...

unable to apply custom css to primefaces components

In my Java EE application, I am using a ready-made template with CSS and jQuery. All the PrimeFaces components are rendering properly except for the panelGrid control of PrimeFaces 3.2. It is being displayed with a border, but I want it without a border. I ...

Angular validation with input binding using if statement

I have developed a reusable component for input fields where I included a Boolean variable called "IsValid" in my typescript file to handle validation messages. Here is the code from my typescript file: export class InputControlsComponent implements OnIn ...

Tips for arranging menu items along a curved counter

One of my clients has requested a unique design feature—a curved menu. The menu items are currently represented by red squares, but they should be positioned to follow the curve of the pink squares on the blue bar. How can I achieve this effect? Should ...

Centering text using CSS Bootstrap

Hey everyone, I've been attempting to center my text using Bootstrap, but for some reason it's not working. Can someone help me figure out what's going on? <footer class="row p-1 mt-3 bg-primary text-white"> <p class= ...

Is there a way to display a loader when an item is selected from a dropdown menu? For example, while data is being fetched from the backend, can we

This is the HTML form division I have created: <div class="col-lg-2 "> <select name="limitCount" id="limitCount" ng-model="limitCount" class="form-control col-md-2 panel-refresh" ...

Tips on centering elements vertically using Zurb Foundation

I'm trying to create a login form with the username and password fields centered in the middle of the screen using the Zurb Foundation package. Unfortunately, I'm struggling to get things aligned vertically. This is the code I have: <!DOCTYP ...

Refining Flask-Generated Table Content with jQuery Filters

I'm currently attempting to render a Jinja2 template that showcases an HTML table and enables dynamic filtering to search through the table content. Unfortunately, I'm facing issues with getting the search functionality to work properly. While th ...

The for loop does not cycle through every element

I'm working on a class project that involves creating custom HTML tags with JavaScript. I have a for loop set up to iterate over each use of the tag, but for some reason, it only seems to loop over every other tag. I'm specifically trying to crea ...

Guide on adjusting padding for jQuery Flot graph for improved styling

I am utilizing jQuery Flot to generate a basic graph. I do not want the axes to display on either side, but this is causing the points to be slightly cut off. Any suggestions on how to resolve this issue? $.plot('#'+id, [ { data: data.values ...

Execute script when on a specific webpage AND navigating away from another specific webpage

I created a CSS code that adds a fade-in effect to the title of my website, and it works perfectly. However, I now want to customize the fade-in and fade-out effect based on the page I am transitioning from. My desired outcome: If I am leaving the "Biolo ...

Is there a way to retrieve the Location header of a 302 redirection URL using PHP?

Seeking a solution to expand shortened URLs universally. I am aware that popular services like bit.ly, TinyURL, and goo.gl use 302 redirection for forwarding users to new destinations. How can a HEAD request be made in PHP to retrieve the "Location" sectio ...

Using JavaScript to create temporary drawings on a webpage that automatically erase themselves

I am curious about how to achieve a scribble effect that self-erases, similar to what is seen on this website: Example: Thank you! I have come across some similar scripts, but I am struggling to understand how to make the scribble disappear after a few ...

Create a sorting feature for a ListView that allows users to organize items by price in both ascending and descending order, as

view.py class HomeView(ListView): model = Item template_name = "home.html" paginate_by = 12 template <div class="header-dropdown"> <a href="#">A to Z</a> <div class="header-me ...

Axis incorporating additional padding for extensive domains

Encountering a peculiar issue with d3 axis generation. When creating a bottom x axis using d3.axisBottom() for ordinal domain values, extra padding is being added at the starting and ending points of ticks on the axis. This happens when the d3.scaleBand() ...

vertical-align applied to table cells creating a floating effect

I have discovered that by adjusting the display property of the containing block to table and enclosing descendant block boxes in boxes with display property set to table-cell and vertical-align property set to top, it produces the same effect as if the fl ...

The height of the iframe with the id 'iframe' is not functioning as expected

I am trying to set the iFrame's height to 80% and have an advertisement take up the remaining 20%. Code <!DOCTYPE html> <html lang="en"> <head> </head> <body> <iframe id="iframe" src="xxxxxx" style="border: ...

Tips for using various versions of jQuery at the same time

Despite searching on Stack Overflow, none of the answers provided seem to work for my issue. My requirement is to utilize two separate versions of jQuery. Below is the code snippet: I first link to the initial version and use the following code: <sc ...

Dynamic drop-down navigation with rearranging menu

I am attempting to design a drop-down navigation bar with 2 rows. Initially, only 1 row is visible. Upon clicking the visible row, both rows should appear. Subsequently, when one of the 2 rows is clicked, only that specific row should be displayed. Below a ...