What is the best way to show the clicked image in a different div using jQuery?

Is there a way to show an image in a larger div when clicked on another div with a smaller size?

Despite multiple attempts and various approaches, I have been unsuccessful in achieving this task. Thus, after hours of effort, I decided to seek help here. Below is my code snippet:

I am looking to showcase an image that is clicked in a smaller slide within a div named largerimage.

$(document).ready(function(){
    $("img").click(function()
    {
        $(".largerimage").show($this);
    });
});

Answer №1

Yes, I believe this solution will work.

$("img").click(function(){
  $img = $(this).clone();
  $(".largerimage").show().html($img.removeAttr('width'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img width="100" src="http://dummyimage.com/300x200/000/fff"/>
<div class="largerimage"></div>

Answer №2

Give this a try for potential assistance

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
      $(document).ready(function(){
        $('img').click(function(){
          $largeimage = $(this).clone().css({height:"300", width:"300"});
          $(".largerimage").html($largeimage);
        });
      });
    </script>
  </head>
  <body>
    <img width="50" src="C:\Users\buffer\Pictures\a.jpg" />
    <img width="100" src="C:\Users\buffer\Pictures\b.png" />
    <img width="200" src="C:\Users\buffer\Pictures\c.png"/>
    <div class="largerimage"></div>
  </body>
</html>

Showcasing three images of differing widths within a larger div with fixed dimensions.

Answer №3

Here is a functioning code snippet:

$(document).ready(function(){
  var height1 = $("div.first").height();
  var width1 = $("div.first").width();
  var area1 = height1*width1;
  var height2 = $("div.second").height();
  var width2 = $("div.second").width();
  var area2 = height2*width2;

  if (area1>area2) {
    $("div.first").click(function(){
      $("img").show();
    });
  }
  if (area2>area1) {
    $("div.second").click(function(){
      $("img").show();
    });
  }
});

Feel free to test it out on this fiddle.

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

Implementing array values into a jQuery graph syntax

After json encoding from php, I have an array that contains information on different categories: [{"Type":"Category","Name":"games","TotalClicks":"162"},{"Type":"Category","Name":"apps","TotalClicks":"29"},{"Type":"Category","Name":"music","TotalClicks":" ...

To insert a new row into a table with an onClick function, along with removing this attribute from all other rows except for the newly added one

I recently created a piece of code that enables the addition of a new row and removes an (onClick) attribute when clicking on an existing row within a table. <table style="vertical-align:middle;margin:20px;" id="table_insert"> <tr id="tra" > ...

The ReactJS code has been compiled without errors, but it is not displaying in the browser

After putting in a significant amount of effort, I successfully managed to install and configure ReactJS. Upon running the "npm start" command, the code was "COMPILED SUCCESSFULLY" and directed me to the web browser. However, there was no output visible; t ...

retrieve data from the API response using a node request

I initiated an API request using the request method. Everything seems to be functioning properly, but I am encountering difficulty extracting the results from the response. The snippet of my code in question is as follows: app.get('/events/:query&a ...

The Bootstrap navigation row smoothly adjusts its size, yet only partially highlights active or highlighted links

When viewing on a smaller screen, one of the links in this navigation (nav-pills, nav-justified) spans two lines, causing the other links to appear larger as well. While this may seem beneficial, active or highlighted links only partially fill the box, res ...

Regular Expression designed specifically for detecting alternative clicks

When using ngpattern for validation, I have encountered an issue where my error message is displaying for a different reason than intended. The error message should only show if the field is empty or contains only special characters. <textarea name="ti ...

AJAX Patch requests in Laravel

My modal is designed to update information related to various countries. // Here is a snippet of the modal HTML code <div id="EditModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div c ...

Building unique CSS classes with Material UI v5 theme - A step-by-step guide

Incorporating Material UI v5 into my application has been a smooth process, except when it comes to using react-d3-tree for rendering SVG tree charts. This library allows for custom CSS class names to be added to the <path> elements that connect the ...

Issue with animated cursor function not activating when used with anchor links

I've spent hours searching for a solution but I can't seem to find one. I'm attempting to modify the codepen found at https://codepen.io/Nharox/pen/akgEQm to incorporate images and links, however, two issues are arising. The first issue is t ...

Is it possible for !# Ajax crawling to result in duplicated content on search engines when used on a non-JS accessible website?

My website functions with both JavaScript enabled and disabled. All links on the page are initially in standard format like <a href="/pagename">. However, if a visitor accesses the site with JavaScript available, the links will be modified to <a h ...

Center an image and superimpose text in the middle of the webpage

I am attempting to center an image with text overlay in the middle of a page. Although the image is centered correctly, the overlay text remains off-center. Below is the code snippet. The text should be positioned in the top left corner of the image. &l ...

Having trouble retrieving dynamic data from a JSON array

I am working with a JSON array. JSON.stringify(ar) When I attempt to display the results in an alert: alert(JSON.stringify(ar)); The output displayed in the alert is: [{"url":"link1","title":"title1"}] However, when I try to transfer the contents i ...

What could be causing the code to produce 4 elements instead of the expected 2?

I'm trying to understand why the code above is creating four new paragraphs instead of just two. Can someone please explain what exactly happens in the $("p").before($("p").clone()); part? <!DOCTYPE html> <html> <head> <script ...

Creating a dynamic logo image in a Bootstrap 4 navbar that seamlessly overlaps while maintaining full responsiveness is a simple yet effective way to elevate

Is it possible to create a responsive overlapping logo on a Bootstrap navbar without affecting the hamburger menu icon? Good day! I am currently using Laravel Spark along with Bootstrap for my project, and I have encountered a specific challenge: How ca ...

Determine the height of the left div, which is set to auto, and apply the same height to the right div, which has overflow set

I am facing an issue that needs a solution. I need to ensure that two div elements have the same height. The left div should have 'auto' height while the right one must match it. Moreover, the right div contains 14 nested divs that need to be scr ...

Selecting multiple buttons from Bootstrap will allow you to retrieve the text of the button that

After getting inspired by the solutions provided on this page, I attempted to create a button group. However, when I click the button, the text displayed in the alert box includes all the buttons that have been clicked. How can I modify it so that only the ...

Experiencing lag with jQuery; looking to identify lingering processes causing slowdown

Recently, I've noticed that my webpage starts to lag after some time of usage. I suspect this could be due to certain jQuery functions not stopping once they are executed. Is there a way for me to identify which processes are still active so I can pin ...

How can I use a CSS file in PHP to conceal the folder structure?

main.css $theme->assign('css_file',SITE_URL.'styles.php?theme='.THEME_ID); $theme->display(TEMPLATES_PATH.'header.tpl'); header.tpl <link rel="stylesheet" href="{$css_file}"> styles.php include TEMPLATES_PATH. ...

Transferring a dynamically created value from a list item to a jQuery function

Currently, I have a list generated by a datatable that contains specific attributes. My goal is to pass one of these attributes to a jquery function, which will then include it in a POST request and redirect the user to another page. However, I am encounte ...

Avoid utilizing variables outside of the ajax callback function

Is there a preferred method to access global variables outside of a callback function? var icon; $(function(){ $.get('data.xml', function(xml){ icon = xml.documentElement.getElementsByTagName("icon"); //this will display a value ...