On click, the current image should be replaced with the thumbnail image

I have been working on a project and came across a codepen link https://codepen.io/robgolbeck/pen/bVGmop that is similar to what I need. However, I am trying to achieve a specific functionality where clicking on a thumbs image replaces the large image with the clicked one and removes it from the thumbs list. I want only 5 images in rotation, but currently there are 6 (5 + 1 repeated in thumbs). I've attempted to modify the JS code without success. Can anyone provide guidance on this issue? I've been stuck on it all morning. Thank you in advance.

JS

$('#thumbs img').click(function(){
  $('#largeImage').attr('src', $(this).attr('src').replace('thumb','large'));
  $(this).attr('src',$('#largeImage').attr('src').replace('large','thumb'));
});

Answer №1

When handling the click event for #thumbs img, start by displaying all images and then hiding the current one. You can begin by simulating a click on the first image.

$('#thumbs img').click(function(){
  $('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
  $('#description').html($(this).attr('alt'));
  $('#thumbs img').show();
  $(this).hide();
});
$('#thumbs img').first().trigger('click');

Answer №2

You can easily utilize jQuery's show() and hide() functions. Begin by displaying all child elements of #thumbs, then hide the currently selected element using $(this).hide();.

To start with hiding the initial image, you can employ

$('#thumbs').children().first().hide();

$('#thumbs').children().first().hide();
$('#thumbs img').click(function() {
  $('#largeImage').attr('src', $(this).attr('src').replace('thumb', 'large'));
  $('#description').html($(this).attr('alt'));
  $('#thumbs').children().show();
  $(this).hide();
});
#thumbs {
  padding-top: 10px;
  overflow: hidden;
}

#thumbs img,
#largeImage {
  border: 1px solid gray;
  padding: 4px;
  background-color: white;
  cursor: pointer;
}

#thumbs img {
  float: left;
  margin-right: 6px;
}

#description {
  background: black;
  color: white;
  position: absolute;
  bottom: 0;
  padding: 10px 20px;
  width: 525px;
  margin: 5px;
}

#panel {
  position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gallery">
  <div id="panel">
    <img id="largeImage" src="http://robgolbeck.com/demos/image-swap/image_01_large.jpg" />
  </div>
  <div id="thumbs">
    <img src="http://robgolbeck.com/demos/image-swap/image_01_thumb.jpg" alt="1st image description" />
    <img src="http://robgolbeck.com/demos/image-swap/image_02_thumb.jpg" alt="2nd image description" />
    <img src="http://robgolbeck.com/demos/image-swap/image_03_thumb.jpg" alt="3rd image description" />
    <img src="http://robgolbeck.com/demos/image-swap/image_04_thumb.jpg" alt="4th image description" />
    <img src="http://robgolbeck.com/demos/image-swap/image_05_thumb.jpg" alt="5th image description" />
  </div>
</div>

Answer №3

It seems like the attr method from jQuery is causing a problem. I recommend using the prop method instead, which can be seen in the code snippet below:

$('#thumbs img').click(function(){
   $('#largeImage').prop('src',$(this).prop('src').replace('thumb','large'));;
    $('#description').html($(this).prop('alt'));
});

Answer №4

$('.thumbnail').on('click', function(){
    
    $(".thumbnail").each(function(){$(this).show();});
    $('#mainImage').attr('src',$(this).attr('src').replace('small','large'));
    $('#details').html($(this).attr('alt'));

    $(this).hide();
});

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

Achieve a Smooth Transition: Utilizing Bootstrap 3 to Create an Alert Box that Fades In upon Click and Fades Out

Incorporating AngularJS and Bootstrap 3 into my web app, there is an "Update" button that saves user changes. Upon clicking the "Update" button, I aim to trigger and display bootstrap's alert box with the message "Information has been saved," followed ...

Is it possible to achieve a fade effect in material-ui that truly hides the component instead of just disabling its visibility? If so, how can this be accomplished?

Currently, I am utilizing the component provided by material-ui, which can be found at material-ui. <Fade in={!randomizeFlag}> <Grid> <FormControlLabel control={<Switch onChange={this.handleStartValueFlag} & ...

Is it possible to prevent the text from appearing in the text box when a button is

I have successfully implemented a feature where clicking on the button (click on me) in HTML displays a textbox along with another button (show) on the screen. The text written in the textbox is visible when the show button is clicked. However, after the i ...

A lengthy string of characters within a continuous paragraph without any interruptions

Is it possible to have a long string automatically move to the next line without breaking any characters in the string, rather than splitting the word between lines? The current display looks like this: My sample text ha s errors as it is display ...

JavaScript Page Search - Error in Finding Single Result

I am currently working on implementing a 'find in page' search box and have come across some JavaScript code that is working really well. Everything works great when there are multiple strings, as pressing enter cycles through the results and the ...

How can you easily center content vertically on a web page?

There has been plenty of back-and-forth regarding this issue, with proposed solutions ranging from pure CSS to pure HTML. Some can get quite complex, involving deeply nested divs and intricate CSS rules. I'm looking for a simple and uncomplicated answ ...

Implementing a Toggle Class Feature in a Function

I am currently facing an issue with a function that triggers an overlay on mouseenter event and needs to be unbound. I want to incorporate a way to close the overlay with a specific 'x' element and allow the user to trigger the event again. Howev ...

unable to view the contents of the template using the directive

I am facing an issue with the Directive, as it is not adding an element to the Template. This directive has been included in the .html file. There are no errors being displayed in the console. directive.js: app.directive('myDirective', function ...

From where was this color of the navigation bar derived?

As I delve into a site previously worked on by someone else, my task is to recreate the navigation they implemented. However, I am unable to locate in the source code what was used for the navigation background. The challenge lies in the fact that after m ...

Discover the power of EJS embedded within HTML attributes!

There are two cases in which I am attempting to use an EJS tag within an HTML attribute. SITUATION 1: <input style="background-image: url(<%= img.URL %>)" /> SITUATION 2: <input onchange="handleCheckboxChange(<%= i ...

Ways to obtain a full number with a leading zero

I have a number that looks like this: 0065921922572 I am passing this value as a parameter to a JavaScript function. When I alert the data, it displays like this: 65921922572 (the leading zeros are removed) I have attempted to convert the number to a ...

Identifying when a fetch operation has completed in vue.js can be accomplished by utilizing promises

Currently, I am facing a dilemma in my Vue.js application. I am making an API call within the created() hook, but there are certain tasks that I need to trigger only after the API call has been completed. The issue is that this API call usually takes aroun ...

Maintain dropdown menu visibility while navigating

I'm having an issue with my dropdown menu. It keeps sliding up when I try to navigate under the sub menu. I've spent all day trying to fix it, testing out various examples from the internet but so far, no luck. Any help would be greatly apprecia ...

I am facing an issue where the data is not being populated in my form even though I am using ng

Here is a form with grouped inputs using the ngModelGroup directive: <form #form="ngForm" (ngSubmit)="submit(form.value)"> <fieldset ngModelGroup="user"> <div> <label>Firstname:</label> < ...

Tips for consistently implementing focusVisible design on a targeted Material-UI Button element:

When using the Material-UI Button component, I want the "focus" styling to match the "focusVisible" styling. This means I want the same ripple effect to be visible whether the button is focused programmatically or with the mouse, as it would be when focuse ...

Stopping all animations with JQuery animate()

I have a question about stopping multiple animations. Here's some pseudocode to illustrate my situation: CSS #div1 { position: absolute; background-image: url("gfx/cat.jpg"); width: 60px; height: 70px; background-size: 50%; b ...

Using Primeng to implement pagination and lazy loading in a dataView

Looking for a way to search through product data with filters and display it using primeng dataview? Consider implementing pagination in your API that pulls products from the database page by page. Set the products array as the value in dataview so that wh ...

Extract Content from an Element Using Selenium in Python

How do I extract the text from an element using Selenium? Element Content: <p class="sc-168cvuh-1 cNxwvb"> <svg aria-hidden="true" focusable="false" data-prefix="fas" ...

What could be the reason that str_replace is failing to replace this particular string?

I am facing an issue with my PHP code that is intended to load data from a CSS file into a variable, find the old body background colour, replace it with a new colour submitted through a form, save the updated CSS file, and update the colour in a database. ...

Experiencing challenges working with the offset class in Bootstrap 4

Recently, I attempted to use offset in my form rows and columns to center them using Bootstrap 4 documentation. Unfortunately, it didn't work as intended and now I'm stuck trying to figure out the problem. Despite looking for similar issues onlin ...