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

Performing a Search Operation using AJAX with Elasticsearch

I've been struggling to find the correct method for requesting data from elasticsearch using a jQuery AJAX call. I keep encountering parsing errors or getting all documents in the index instead of my intended results. $(document).ready(function() ...

In PHP, how to dynamically set multiple selected values in Select2 while loading data via ajax

Hey there! I'm trying to display some data in a multiple select2 and have it pre-selected. The code below works perfectly for fetching all the data, but I'm stuck on how to include the selected information. In my database table "ejecutivo_agenci ...

What is the process for inserting a scroll bar within a div element?

   I have recently created a webpage using some divs, along with a bit of CSS and JavaScript. I am struggling to figure out how to add a scrollbar to one of my divs. The code is not overly complex, as it includes both CSS and JavaScript. <html> & ...

The masonry filter is functioning only for a singular time

I have successfully implemented multiple filters using the jQuery Masonry plugin. However, I am facing an issue where once one filter is used, another filter cannot be applied until the page is reloaded (I am still learning about this). Below is my code: ...

Guide to modifying the behavior of a dynamic dropdown menu

Jquery: $('body').on('change', '.combo', function() { var selectedValue = $(this).val(); if ($(this).find('option').size() > 2) { var newComboBox = $(this).clone(); var thisComboBoxIndex ...

When hovering over A, the DIV element fails to appear

I'm encountering an issue with a specific page on my website () The problem lies with a .dropdown class that is supposed to display a DIV right below the header. In the source code, you can find it underneath <-- START DROPDOWN MENU -->. When I se ...

Can I position two div elements next to each other, with one div partially offscreen, without utilizing display none or setting its width to zero?

Is it possible to position one div outside of the screen width while setting two divs side by side? I am looking to insert DIV2 above DIV1 with animation without adjusting its width or display. The goal is for DIV2 to enter the screen from the side alread ...

When I attempt to press the shift + tab keys together, Shiftkey is activated

Shiftkey occurs when attempting to press the shift + tab keys simultaneously $("#buttonZZ").on("keydown",function (eve) { if (eve.keyCode == 9 && eve.shiftKey) { eve.preventDefault(); $("#cancelbtn").focus(); } if (eve. ...

Coloring a table in vue.js based on performance rankings

I'm facing an issue with sorting my data performance in vue js. Here is the script I have created so far: <template> <div> <div class="row"> <h2> Campaign Performance </h2> <table class=&q ...

Utilize Python to extract information from an HTML table

I am looking to extract data from an HTML table using a Python script and save it as variables that can be later utilized in the same script after loading them in if they exist, into a separate file. Additionally, I would like the script to disregard the f ...

How to enhance a jQuery tab control by implementing custom JavaScript forward and back arrows?

Although there are other questions related to this topic, mine is unique. I have a scrolling jQuery feature that utilizes tabs for selection and is automated. Modifying the layout is challenging because it is dynamic and depends on the number of items in t ...

Footer overlapping occurs on a single page

My webpage is having an issue where the footer is overlapping. It seems to be fine on all other pages except for this one. I would prefer not to make any changes to the footer since it's working correctly on other pages. Is there a way to adjust the C ...

By utilizing the <tr> element, one can enhance the border thickness by 1 pixel

Is it possible to ensure that row selection in a table is consistent in terms of border alignment on both sides? https://i.stack.imgur.com/qmZiQ.png If you have any suggestions or solutions, please share them. Thank you! table { margin: 10px; bord ...

What could be the reason for the malfunction of jQuery's show() function?

Using jQuery, I have implemented a functionality to hide a div using the hide() method. However, upon clicking a link, the div is supposed to show but unexpectedly disappears after appearing briefly. HTML Code Snippet <div id="introContent"> & ...

Hover events in the browser are currently operating at a sluggish pace

When I hover over a photo, an overlay is supposed to appear. However, if I move the mouse quickly (from the top to the bottom of the page, for example), the overlay remains visible even after I stop hovering over the photo. I have tried different event ha ...

Utilize the jQuery queue method to trigger an action following the completion of a toggleClass

I am working on a sliding content element that should display its contents when clicked. Below is the code snippet: <div class="boxwrap hidden-sm hidden-xs"> <div class="boxright pull-left"> <a href="#" id="toggleContactOpen" class="pull ...

Using jQuery's .remove() function removes all of the children from the container, rather than just one at

For my latest project, I am focused on creating a seamless full-page transition using AJAX calls. The challenge I'm facing is removing content from the DOM when loading in a new page. Despite adding a unique class or ID to the element, the .remove() f ...

The calculation of Value Added Tax does not involve the use of jQuery

My form setup is as follows: <form method="post" action="" id="form-show"> <table class="table table-bordered table-striped table-hover" id='total' style="width:100%;"> ...

Manipulating the content of a specific row's table cells with a text box in jQuery Datatables

I am working with a jQuery datatable that consists of only one column. Every time a row is selected, a panel opens with a text box that automatically populates with the name of the selected td. I am trying to figure out how to edit the name of the selected ...

Enhance your Divi theme with Elegant Themes: Explore mobile-friendly background image zoom effects

I have a regular section module with an image as the background. The design looks great on desktop but appears zoomed in and blurry on mobile devices. I'm struggling to understand why the mobile view is not adjusting properly, and I am unsure if there ...