Switching the entire div when hovering and switching back when hovering out

I have a table and I am trying to change one div to another div when hovering over it, and then change it back when the hover out event occurs.

Here is my table:

<table id="table2">
<body>
  <tr>
     <td>
        <div id="com.zynga.wwf2.free" class="redips-drag orange" style="border-style: solid; cursor: move; background-image: url(https://lh3.googleusercontent.com/mm9kB_B7UTEMuG2t914nmu2pox-G5a64GlajDaUdf7M5LfKBeYpBbeouyRVgsBATLSA=w300); background-size: 100px 100px; background-repeat: no-repeat;  width: 100px;
           height: 100px;"></div>
     </td>
  </tr>
</body>
</table>

Now, I want to replace the div on hover with this code:

<div><span>clicks:123</span><br><span>money:7890</span></div>  

and change it back to its original state on hover out.

This is what I have tried:

var temp_html;
    $("#table2 tr").find('div').hover(function(){
          var temp_html = $(this).clone().wrap('<div>').parent().html();

        $(this).replaceWith("<div><span>clicks:123</span><br><span>money:7890</span></div>");

        }, function() {
       $(this).replaceWith(temp_html);
    });  

Can someone help me identify where I am going wrong?

Answer №1

Why not try placing both of these divs within a table and toggle their visibility?

HTML

<td id="cell">
  <div id="first-div"></div>
  <div id="second-div" class="hidden"></div>
</td>

CSS

.hidden { display:none; }

jQuery

$('#cell').on('mouseenter', function() {
   var currentCell = $(this);
   currentCell.find('#first-div').addClass('hidden');
   currentCell.find('#second-div').removeClass('hidden');
}).on('mouseleave', function() {
   var currentCell = $(this);
   currentCell.find('#first-div').removeClass('hidden');
   currentCell.find('#second-div').addClass('hidden');
})

A BETTER APPROACH


Simplify by toggling the class of the hovered element and utilizing CSS for styling!

HTML

<td id="cell" class="visible-first-div">
  <div id="first-div"></div>
  <div id="second-div"></div>
</td>

CSS

#cell.visible-two #first-div { display:none; }
#cell.visible-first-div #second-div { display:none; }

jQuery

$('#cell').on('mouseenter', function() {
   $(this).removeClass('visible-first-div').addClass('visible-two');
}).on('mouseleave', function() {
   $(this).removeClass('visible-two').addClass('visible-first-div');
})

Answer №2

To achieve this effect, you can utilize CSS exclusively with the :hover pseudo-class:

td .redips-drag + div, td:hover .redips-drag {
    display: none;
}
td:hover .redips-drag + div {
    display: block;
    min-height: 100px; /* adjust min-height to be equal to the tallest element's height for seamless transition */
}

DEMO

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

Veracode Scan finds vulnerability in jQuery html method with Improper Neutralization of Script-Related HTML Tags in a Web Page error

Veracode has flagged the issue Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS) within the following line of code. $('#SummaryDiv').html(data); $.ajax({ url: 'Target_URL', type: &a ...

The touchend event and target _blank are not working together harmoniously

With both image and link hover states in place, I've implemented a piece of code to ensure that opening a link on iOS only requires a single tap. Check out the code below. However, I've encountered an issue where using a link with target="_blank ...

The website that had been functioning suddenly ceased operations without any modifications

It seems like this might be related to a JavaScript issue, although I'm not completely certain. The website was working fine and then suddenly stopped. You can find the URL here - Below is the HTML code snippet: <!DOCTYPE html> <html> ...

Leveraging async/await to handle the insertion of SQL data

function Scan_Insert_Recursive (dir_path) { //scans dir_path recursively and inserts file path into a temporary list Scan_Recursive(dir_path, (err, files) => { //using npm recursive_readdir for scanning if(err) { console.log(err) ...

Displaying items using the flex property can cause the top resize feature to be unreachable

My issue revolves around the display of input errors, which are causing the top section to become inaccessible. I am looking for a solution that will align the content both vertically and horizontally in the center. The challenge arises when the card conta ...

open a new window with a reference based on the name

In order to obtain a reference to the currently open window, I utilize the following code: var refWindow = window.open("mypage1", "name_mypage"); If I wish to close the window, I simply use this command: refWindow.close(); When I refresh the screen (by ...

Interactive tooltips within a list of elements in a leaflet map

I am trying to implement a feature where a popup is displayed on mouse hover, showing names that are selected from an array list. I have placed multiple markers on a map at different latlon positions and now I want to display a popup containing the name ...

Utilize AngularJS's OrderBy feature to iterate through the list for navigation in ascending order by Index

I am looking to customize the order of an Object-array using ng-repeat based on their index keys in ascending order. $scope.paneContainer = {books:[{}] } ; $scope.paneContainer.books[0].science = { title:"science", content:"web/app/views/partials/scienc ...

Challenges encountered in exporting functions with Webpack 5

Imagine having an exported function called createTableFromJSON(json) from the main file named index.js. The configuration example below adheres to the guidelines outlined in Webpack's official documentation: const config = { entry: './assets/ja ...

What is the sequence of Javascript Execution for the event of window.location.href?

After browsing through this discussion on asynchronous JavaScript location.href call and watching a video explaining the event loop, I found no clear explanation regarding the behavior of href within window.location: The script order in the source code is ...

Creating dynamic django modal popup forms with AJAX integration for server-side data processing

Looking for innovative solutions and modern patterns or Django apps to efficiently handle server-side forms that appear in a popup window. Here's what I'm after: User triggers a modal popup window through an action. The form ...

Adjust the maximum and minimum values on a dual thumb slider

I have implemented a two thumb range slider to define the maximum and minimum values. However, I recently discovered that it is possible for the thumbs to cross over each other - the max value can exceed the min value and vice versa. I am looking for a s ...

What causes the "d3" to be undefined in the Chrome console of my React application?

Currently, I have imported the d3.js library into my React application using the following code: import * as d3 from "d3"; I have successfully created a basic world map that displays correctly in the browser. However, when attempting to execute the follo ...

Issue with [rowHovered] directive in PrimeNG Table when cell-background is defined

I am working with a scrollable TreeTable where I have defined the rowHover attribute in the following manner: <p-treeTable [value]="items" [columns]="scrollableCols" [resizableColumns]="true" [frozenColumns]="frozenCols" [scrollable]="true" scrollHeigh ...

Is there a way to consistently monitor the visible image's height?

I am facing an issue with a slider of images where I need the height of the slider to always match the height of the currently visible image within the slider. I have tried implementing code that automatically detects the height of the image within the sl ...

Steps for adjusting an image in a Bootstrap Carousel

Currently tackling a simple bootstrap Carousel for a school project. I've adjusted the max-height of the images to 400px but it seems like only the top part of the images is visible. I attempted to adjust the margins on the image, and even thought abo ...

What is the best way to convert a JSON string from a specific array into an array object?

JSON object: { "data": { "myStatus": "[1 2 21 0 50 0 0 0],[2 1 3 1 50 0 0 0]" }, "success": true } Upon converting to the JSON object, it appears that the myStatus is being treated as a string. The goal is to parse it as an Array inst ...

Steps to making an overlapping sidebar with Bootstrap column

I'm trying to set up a sidebar in Bootstrap that overlaps with a column. The goal is for the sidebar to appear when a button is clicked and disappear when the close button x is clicked. Despite using a Bootstrap row and column layout, I encountered is ...

Creating a custom bookmarklet that utilizes document.body.search()

I'm currently working on a bookmarklet that scans the body of a webpage for a specific text string and triggers a script when it finds a match. My code snippet document.body.search(); is resulting in an error message. [Error] TypeError: undefined is ...

The message appearing on my screen reads: "Attempting to read properties of an undefined value, specifically 'map'."

I am attempting to create a map of my various people, but I keep encountering an error with the title. I'm having trouble understanding where the issue lies, here is my code snippet: export const Testt = ({ childs}) => { console.log(childs) ...