Having trouble with implementing jQuery fadeIn for thumbnail images inside a div?

I'm trying to create a functionality where clicking on a thumbnail image in a div will display the full-size version of that image in another div. I want to implement this using the jQuery fadeIn function, but my code isn't working and I am not very familiar with jQuery.

Can someone help me figure out what's wrong with this code?

<html>

<head>

  <script src="http://code.jquery.com/jquery-latest.js"></script>


<style type="text/css">
.imgdiv {

    /* initially overlay is hidden */
    display:none;

</style>    

</head>

<body>

    <div id="book">
    <img src="thumbnails/book1.png" width="100" height="123" rel="photo1"/>

    <img src="thumbnails/book2.png" width="100" height="123" rel="photo2"/>
    </div>


<div class="imgdiv" id="photo1">
    <img src="thumbnails/booklarge1.png" />
</div>

<div class="imgdiv" id="photo2">
    <img src="thumbnails/booklarge2.png" />
</div>        

<script>
    $('#book img').click(function() {      
      $('#book img[rel]').fadeIn('slow'); 
    });       

</script>

</body>
</html>

Answer №1

Update the content within your click event handler to the following:

$("#"+$(this).attr("data")).fadeIn('slow');

For more details, check here.

Answer №2

In my opinion, it is more efficient to utilize html5 data attributes instead of rel. Consider using something like data-largeimage="#photo1"

Furthermore, ensure that the click event is defined within a document ready function: $(function(){ //execute code })

An expansion on Naveed's script:

$(function(){

 $('#book > img').click(function() {      
  $("div.imgdiv").hide();
  var largeImage = $(this).data('largeimage');
  $(largeImage ).fadeIn('slow'); 

 }); 

});

If you prefer to stick with your previous selector, simply change $('#book img[rel]') to

$('#book img[rel="' + variable + '"]')

Answer №3

Easy way to do it:

 $("#popup > img").on("click", function() {      
      $(".image-container").hide();
      $(this).fadeIn('slow'); 
 }); 

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

Trouble assigning the 'data' attribute to the 'Object' tag using jQuery. [Limited to IE8]

I have encountered a problem when creating an object element dynamically in jQuery to display some content. The code functions perfectly in all browsers except for IE8. Here is the code snippet: j$(document).ready(function(){ j$('.ob ...

Show the JSON information found on a specific website URL

I need help figuring out how to display a json file on a web address so that Swagger UI can recognize it. When I visit , I see json data and can save it as a .json file. However, I'm struggling to replicate this using html. Here's what I've ...

Retrieving crucial information from mySQL using jQuery

I have the following jQuery function: $.ajax({ type: "POST", url: "db.php", data: "word="+ tmpWord, success: function(){ //get the word here somehow ^_^ } }); Here is a snippet from db.php: $word = ...

What are some ways to customize the appearance of my ReactJS application while it's being viewed on Chrome on

I'm currently in the process of running my ReactJS app on a Panasonic Vierra Smart TV browser. After importing core-js and babel-polyfill, I managed to load the webpage but encountered an issue with the UI alignment. The styling does not display corre ...

Prevent the href from navigating to the next page when a doubleclick event listener is triggered

In my project, I am using an anchor tag that redirects to another page. However, if the user double clicks on it, I want to prevent the default behavior and stop the redirection. <a href="{location}">My Link</a> element.addEventListen ...

Using the model data in an MVC application with jQuery: A step-by-step guide

I would like to iterate through a model collection in Razor and then use that collection in jQuery on the client side. I am looking for something similar to this: var MyArray = @item.TagName, and then I want to be able to utilize MyArray with jQuery on t ...

Encountering a 400 Error and CORS problem with Magento 2 Token Authentication

When I use token authentication in my app to access the magento API, everything works fine with postman. However, when I try to implement it using jQuery post, I consistently receive a 400 error due to lack of support for the HTTP verb OPTIONS for prefligh ...

Using jQuery.dataTables to choose all rows except the final column

I'm utilizing a table with jQuery.dataTables. I have a requirement to input dates in the last column. However, when I click to input a date, the row becomes deselected. The following code is meant for selecting all rows: Displayed below is the DataT ...

establishing irregular edges for the div container

Looking to create a class that can transform an element into a specific shape using CSS. Not entirely sure if this is achievable with pure CSS alone, but applying a class to the image element will morph it into the desired shape as shown below. <style ...

An interactive top navigation bar paired with three dynamic columns housing scrollable content, all crafted seamlessly with Bootstrap 4

I recently updated my layout using bootstrap but I am facing a glitch. I want the layout to have fixed scrolling only within the columns, without affecting the entire page. Everything works fine until I add some text inside any of the columns, then the who ...

"Utilizing the power of ng-click to target specific child

I am facing an issue with my owl carousel where events are not firing on cloned items. In search of a solution, I came across a suggestion from Stack Overflow to move the event handler from the direct target to its parent element: Original code snippet: ...

Unlock the secrets of creating an interactive chat room effortlessly by harnessing the power of J

In search of implementing a unique chat room using PHP and JavaScript (Jquery) offering group chat as well as private chat functionalities. The challenge lies in finding a way to continuously update the interface seamlessly, while also displaying 'X ...

I am having trouble getting my HTML to properly link with my CSS while using Chrome OS

My website code is not linking with the CSS file <!DOCTYPE html> <html> <head> <link href="main.css" rel="slylesheet" text="text/css"> <title></title> </head> <body> < ...

The absence of underlines for <a href> and <u> is causing an issue

Below is a snippet of code that I am working with: echo "<form> <div id=\"error\"align=\"center\">"; echo "You've either entered the incorrect <b>username</b> or incorrect <b>passwor ...

What is the best way to adjust the size of a div element so that it is

I am facing an issue with a table that contains a TreeView within a cell. The code snippet is shown below: <style> #leftPanel { width:60px; height:'100%'; border:1px solid red; ...

Is concealing a button an effective strategy?

Recently, I decided to design a login form for my website. To quickly style the form, I opted to use Bootstrap through CDN. While working on the form, I encountered an issue with the button size as the device width changed. Initially, I applied the classes ...

Encountering an unusual hash code when implementing Google Tag Manager in a Next.js project was

I am currently using Next.js and have added Google Tag Manager through a script <script dangerouslySetInnerHTML={{ __html: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var ...

I am experiencing an issue where certain HTML classes are not applying the CSS code properly within my PHP file

I am facing an issue with my code where some of the HTML classes are not applying the CSS styles correctly, such as the first class "class="foot"". Below are the files that I have: * { margin: 0%; padding: 0%; ...

Select a specific division element using a pseudo element

My goal is to specifically target the third div in my HTML code with a class called .counter-wrap. When viewed on a mobile device, this particular div has a margin-bottom of 40px. I am looking to eliminate the margin-bottom for only the third stacked div n ...

The issue with Jquery blur function not triggering inside a bootstrap modal

I am attempting to implement a blur function and show feature, but the show function is not working. The element is hidden by default when the document is ready. $(function() { $("#chargeFee").blur(function(){ if($("#chargeFee").val()>0) { ...