"Troubleshooting issue: Changing the image src with jQuery

Seeking assistance for an issue I am facing. I am trying to change an image upon clicking on it. It works smoothly without any animation, but when I add animations, the transition becomes jerky.

<script>
  $("#thumbs img").click(function() {
    $("#mainImage").attr('src',$(this).attr('src').replace('thumbnails','ovado').replace('png','jpg'));
  })
</script>

Adding an animation script causes the same image to load every time. Please take a look at the code below and provide assistance if possible.

<script>
  $("#thumbs img").click(function() {
      $("#mainImage").fadeOut(1000, function() {
          $("#mainImage").attr('src',$('#thumbs img').attr('src').replace('thumbnails','ovado').replace('png','jpg'));
      }).fadeIn(1000);
      return false;
  });
</script>

Answer №1

It seems that there may be multiple images inside the container with the id of #thumbs. To achieve what you want, you should continue passing the element into your next function just like you were doing before.

  • The $(this) in your initial code snippet represented the image that was clicked
  • By referencing it within the click event function, you can effectively pass it to the fadeOut function

I've provided an example that demonstrates what I believe you are trying to achieve. Don't forget to include the replace function back into your code.

var $thumbImgs = $("#thumbs img");
var $mainImg = $("#mainImage");
$thumbImgs.click(function() {
  var $imgClicked = $(this);
  $mainImg.fadeOut(1000, function() {
    $mainImg.attr('src', $imgClicked.attr('src'));
  }).fadeIn(1000);
});
#mainImage {
  max-width: 200px;
}
#thumbs li {
  display: inline-block;
  max-width: 100px;
}
#thumbs li img {
  max-width: 100%;
  height: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id="mainImage" src="https://placekitten.com/g/500/500">
<ul id="thumbs">
  <li>
    <img src="https://placekitten.com/g/200/300">
  </li>
  <li>
    <img src="https://placekitten.com/g/400/300">
  </li>
  <li>
    <img src="https://placekitten.com/g/500/300">
  </li>
</ul>

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

Transferring identifier from ashx page to aspx label or hidden field

I am working on an autocomplete textbox that retrieves data from a database and displays results as the user types. using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select (Patient_First_Name+' '+Patient_Last_Name+' '+ ...

Tips for arranging buttons horizontally in Angular

After adding a third button, I noticed that the buttons were now displaying in a column instead of a row. html: <div class="creator-button-container" *ngIf="isCurrentUserTheChannelCreator"> <div *ngIf="isChannelE ...

What is the best way to extract information from a .txt file and transform it into an integer format?

I am currently working on a project involving a distance sensor that retrieves data from a Raspberry Pi and displays it on a website. The goal is to have the data stored in a .txt file and then transferred to the website for display. I am seeking assistanc ...

Browser crashing due to drag-and-drop upload of desktop images

I've set up a form for uploading images using ajaxForm. I added a feature where users can drag and drop photos from their desktop (HTML5 drag & drop). Everything works smoothly when the photo is small, around 2mb. However, an issue arises when attempt ...

Using Bootstrap 3 with ResponsiveSlides.js

I've been struggling to center the ResponsiveSlides slider within a bootstrap column. Despite resizing my large images to 80% in the CSS, they still refuse to align properly since they're no longer fitting the entire screen at 100%. Can someone l ...

Pressing the enter key will submit the form

After receiving feedback from users in my live chat room, one common request was to add a feature that allows them to send a message by pressing the enter button, instead of creating a new line in the text box. Despite attempting to implement some jQuery ...

What methods can be used to ensure that the variable $ can serve as both an object and a function?

One interesting feature of jQuery is how the variable $ can function as both an object and a function. // For example, you can access object properties like $.fn; // or $.isReady; // You can also call the function $ like this $(); So, how can we make the ...

Adjust the transparency level of the image's mapped area

I need help with changing the opacity of a specific area on an image map when clicked. The image has three areas, but I only want to target and change the opacity of the test2 area regardless of which area is clicked. Since my knowledge of jQuery syntax is ...

Linking issue with href tag

I am having trouble with my download links for PDFs of my Chinese articles. I've tried different approaches, but none of them seem to work. I even attempted linking it to my homepage as a test, but still no luck. <link href="http://unique-domain.c ...

Tips on retrieving the ul list value dynamically using jQuery when it changes

I have a list of countries with their dial codes and country codes. I need to find out which country is currently selected and retrieve its data-country-code using jQuery. Can anyone provide guidance on how to accomplish this? <ul class="country-list ...

Utilizing Loops to Generate Unique CSS Designs on an HTML Page

View reference image ->Take a look at the reference image provided. ->In the image, a for loop is used to create box designs and displayed above. ->The goal is to change the background color and border color of all boxes using a single HTML cla ...

Applying a distinct CSS style to the final entry of each list item

Is there a way for me to assign a different style for the last entries in my code? <?= $k % 2 == 1 ? 'news-figure' : 'news-figure-b' ?> ...

Transforming HTML content in real-time using Express.js

I'm a little uncertain if I understand the concept of Express MVC correctly: If my goal is to create a single page application and dynamically modify the HTML, can Express assist me with this? Or will I only be able to work with static pages that req ...

Automatically fill in the Modal popup

Today, I encountered a challenge while trying to keep the current page in a jQuery data table. Clicking on the "Edit" link in a row would open a modal pop-up and fill in the controls. However, this action resulted in a postback that set the page back to pa ...

Implementing a time delay for submitting an ajax form

I am looking for a way to delay the submission of a form by 2 seconds. I have tried different solutions, but none seem to work for me. I attempted using setTimeout to set a delay, but it is not working as expected. Here is my script and the form tag - wh ...

event fails to trigger on a fixed positioned element

Here's the code snippet I've been working with: if ($('body.page-service-map').length || $('body.page-contact').length) { $(document.body).append('<div class="black-overlay"></div>'); ...

Sending multiple PHP arrays as part of an AJAX response

I have been attempting to send multiple arrays in an Ajax request response (get), but have been struggling to achieve it. Here is the PHP code I want to include in the Ajax request response: echo json_encode($catdata); echo json_encode($productdata); ech ...

What is the best way to conceal a specific class of DIV within a container, and how can I also hide another DIV within the same container by

I have a DIV class containing around 10 elements within a container. I am looking to implement a feature where these elements are hidden and shown one by one every 15 seconds using jQuery with a smooth fadeOut() effect. Your assistance in achieving this wo ...

Unequal height among each div's elements

I'm struggling with creating a footer divided into 3 blocks, but they do not have equal heights, causing the border-right line to appear uneven. Here is a screenshot of the issue: Not equal height of elements Any suggestions on how to fix this? Is m ...

During operational hours, an Ajax request may cause interruptions to the website's functionality

Having an issue with a large ajax request: I am querying a PHP file that makes some cURL requests, taking 15-20 seconds to complete and then returning JSON on my webpage. It's a standard ajax request, but I found a strange bug. While the ajax query i ...