Using JQuery and Bootstrap: Adding an image from a selection of images to a carousel

As a beginner in the world of JQuery, I am currently working on creating a customizable carousel where users can select pictures to add to the carousel with just one click.

On the left side of the page, there is a selection of images to choose from, while the carousel itself is displayed on the right side. Below is an example of the code used for the list of images:

<div class="pics">
  <div class="col-sm-4 iu">
    <img src="img/iu.png" alt="IU" class="img-thumbnail" style="width:150px; height:150px;">
  </div>

  <div class="col-sm-4 hyuna">
    <img src="img/hyuna.png" alt="hyuna" class="img-thumbnail" style="width:150px; height:150px;">
  </div>

Here is the code for the carousel slides:

<div class="carousel-inner" role="listbox">
  <div class="item active">
    <img src="img/minah.png" alt="minah" />
  <div class="carousel-caption">
    <h1>This is Minah.</h1>
  </div>
</div>
<div class="item">
  <img src="img/juwoo.png" alt="juwoo" />
  <div class="carousel-caption">
     <h1>This is Juwoo.</h1>
  </div>

Upon loading the page, a default carousel is already present and it's designed to clear out completely when an image from the list is clicked.

Answer №1

This is just a rough idea of how it might work:

// When the document loads, set a variable to indicate that no image has been clicked.
var image_clicked = false;
$(document).on("click",".img-thumbnail",function(e){
  // Once you click an image on the side.
  var $this = $(e.target)
  // Check the variable
  if(!image_clicked){
    // If not clicked, clear the carousel
    $(".carousel-inner").html("");
    // Mark it as clicked, so that the second time you click an image, it won't enter this block again.
    image_clicked = true;
  }
    // It will append the new image to the carousel.
    // On the second click, you will only be appending elements to the carousel.
    // Add item div to the carousel.
    $(".carousel-inner").append("<div class='item'><div class='carousel-caption'><h1></h1></div></div>");
    // Get the added items
    var item = $(".carousel-inner").find(".item");
    // Get the current item and prepend an image to it.
    $(item[item.length-1]).prepend($this);
    // Remove the previously active class that was assigned
    $(".carousel-inner item").each(function(i,it){
      if($(it).hasClass("active")){
        $(it).removeClass("active");
      }
    });
    // Add an active class to the currently added item
    $(item[item.length-1]).addClass("active");
    // If your images on the side have a value attribute
    // then use it as a caption
    $(item[item.length-1]).find(".carousel-caption h1").html($this.attr("value"));
});

I haven't tested this code, but it should give you an idea of what you are attempting to achieve.

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

MERN Stack deployment to Heroku: Remote rejected - unable to push changes to master branch (pre-receive hook declined)

Just a heads up: I've successfully deployed using this method around 3 times in the past, but now it seems to be failing. Could there have been an update with Heroku that's causing this issue? Not entirely sure... I'm attempting to push my ...

Hovering over text can change the width of the background color

I'm currently facing a roadblock while working on my website. I have successfully adjusted the height of the list items on my navigation bar, but now I am struggling to expand the width so that when hovering over it, the background color covers a slig ...

Utilize a pre-defined layout in a Vue component that is sent as a property

As a complete beginner, I kindly ask for your patience as I navigate through the coding basics. I am looking to utilize a template specified in the prop. The template is located within the DOM. My intention for approaching it this way is to reuse the comp ...

How about incorporating AJAX into your HTML code?

I am currently in the process of developing an email system for a company that wishes to use it for sending emails. To achieve this, I have implemented a custom HTML editor for creating email content. My goal is to post the email contents to an external PH ...

Determining the Similarity of jQuery Selectors' Selected Elements

I'm looking for a way to programmatically identify if two jQuery selectors have chosen the exact same element. My goal is to iterate over multiple divs and exclude one of them. This is what I envision: var $rows, $row, $row_to_exclude; $rows ...

The text exceeds the width of the paragraph

Something very odd is happening, a rare occurrence in my over 20 years of web development experience. The text within the paragraph seems to be overflowing its boundaries: https://i.sstatic.net/bmB1zg7U.jpg My client specifically requested the Duffy Scri ...

Having trouble loading CSS and JavaScript files in CodeIgniter?

In my project, I am utilizing Bootstrap as a template. However, when attempting to access Bootstrap in Codeigniter, the page fails to load the CSS and JavaScript files. I have included the URL in autoload.php $autoload['helper'] = array('url ...

Spotfire: Changing a URL in an input field after it has been entered by the user

Currently, I am faced with a challenge related to importing a file in spotfire based on the path provided by the user. To accomplish this, I am utilizing a data function written in R script. In R, it is essential to note that all "\" characters are n ...

Managing two variables in C# Controller and View

I am facing an issue with the two variables in my controller class. The first variable, currentUserId, is supposed to store the user currently logged into the website. The second variable, currentRoomId, should track the chat room the user is in. The probl ...

Is it possible to implement a jQuery function instead of utilizing an iframe? Iframe tends to have a longer loading time, so using a

Would it be possible to achieve the same functionality as an iframe using the onload function and trigger function in jQuery with a div or anchor tag? I'm looking for alternatives to iframes because they take too long to load. Here is the current cod ...

Show the HTML that is received as JSON when making an AJAX request

Previously, my AJAX call would return an array of data as JSON by using "echo json_encode(array)" in my PHP code. I would then loop through the result in my script to build HTML with the returned data before displaying it. Recently, I updated the code to ...

Execute a task on the elements contained in an array within a collection of arrays

I'm grappling with a challenge that involves calculating the sum of the products of two elements that are housed in separate objects. Essentially, I'm seeking to determine TotalValue through the formula: TotalValue = Sum[Arrays.close * List.amtP ...

Creating an Edit and Delete feature for a looped textbox in JavaScript

Exploring the realm of web development, I am on a mission to integrate an add/edit/delete feature to manipulate form values using Javascript. Let me shed some light on what I aim to achieve. Within my codebase, there exists a loop that sequentially displa ...

Utilizing ng-switch for handling null values or empty strings

Can anyone assist me with this issue? I have a variable called $rootScope.user = {name:"abc",password:"asd"}. When a service response occurs, I am dynamically creating a rootscope variable by assigning it. How can I use ng-switch to check if the variable h ...

The issue with Jquery getJSON not functioning properly across different websites

I am having an issue with a JavaScript code that fetches JSON data. It works perfectly fine when run locally, but does not function properly when accessed from another website. Below is the script: $(function(){ var aT = new AjaxTest(); aT.getJso ...

Differences between importing {fn1} from 'lib' and importing fn1 from 'lib'

When it comes to importing functions from lodash, I have been advised by my coworker that it is more efficient to import each function individually rather than as a group. The current method of importing: import {fn1, fn2, fn3} from 'lodash'; ...

Debug errors occur when binding to computed getters in Angular 2

Currently, I am integrating Angular 2 with lodash in my project. Within my model, I have Relations and a specific getter implemented as follows: get relationsPerType() { return _(this.Relations) .groupBy(p => p.Type) .toPairs() ...

in comparison to the base directory in ASP.NET

When using "/", it refers to the root directory. This can be seen in code like: <link href="/Styles/Order.css" rel="stylesheet" /> This specifies a file path that is relative to the root directory. However, when dealing with server controls, you m ...

Using Async/Await for Timers within a Loop in Javascript

Currently developing a "Timeblocks" style application for university. The main concept involves having a list of subtasks, each with a specified time limit. The goal is to iterate through these tasks, utilizing a timer to countdown the allocated time and t ...

What is the best way to incorporate code into an Alexa Skill?

I'm encountering significant hurdles in grasping how to transform my Python or JS code into a functional skill for Alexa. I have snippets of code written in Python and partially in JS, yet despite my efforts, I am unable to incorporate it into an Alex ...