Generate a div element that is positioned at the exact same location as the image

I have successfully created an interactive image slider. When a user clicks on an image, I am using JavaScript to dynamically generate and display a div with additional text content. However, I am facing an issue where the newly created div appears on the very right side of the page instead of being positioned in line with the selected image. How can I ensure that it occupies the same position as the image?

Below is the code snippet I am currently using to create the div and insert the image:

function flipIt(obj){
    console.log("value before Function status   "+status);

    $(obj).wrap($('<div class="foobar"/>').css({
        "position" : "absolute",
        "left"     : $(obj).position().left,
        "top"      : $(obj).position().top,
        "height"   :$(obj).height(),
        "width"    :$(obj).width(),
    }));

    alert('classes ..'+$(obj).hasClass("foobar").toString());
    $(obj).animate({"left": "-=40px","opacity": "0.65"},"slow");
    $(obj).animate({"height":"600px","width":"320px"},30);

    $(obj).css("-webkit-backface-visibility","hidden");
    $(obj).css("-webkit-transform-style","preserve-3d");
    $(obj).css("-webkit-transition","all 1.0s linear");
    $(obj).css("transform-style","preserve-3d");
    $(obj).css("transition","all 1.0s linear");

    $(obj).css("-webkit-transform","rotateY(180deg)");
    $(obj).css("transform","rotateY(180deg)");

    status=0;
    console.log("after if value set status   "+status);
}

CSS styling for the .foobar div:

.foobar {
    -webkit-backface-visibility:visible;
    position:absolute;
    color:red;
    background-color: red;
    background:red;
    content: div is here;
    height:450px;
    width:250px;
}

How can I position the dynamically created div at the exact location of the selected img?

Answer №1

$("#id").html("<div class='foobar'>This is div</div>")

Can you identify the id of the div element where the image can be found?

Answer №2

Check out this code snippet:

 $("#id").wrap('<div class="foobar"/>');   

View 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

If the cursor hovers over an image, the onmouseover function does not work properly

Currently, I am developing a PHP page to display data from a database in a tabular format. Each column in the table represents an 'ATC Station'. When active, additional data is displayed upon hovering over the cell. Everything was running smooth ...

A user-friendly JavaScript framework focused on manipulating the DOM using a module approach and aiming to mirror the ease of use found in jQuery

This is simply a training exercise. I have created a script for solving this using the resource (function(window) { function smp(selector) { return new smpObj(selector); } function smpObj(selector) { this.length = 0; i ...

"What is the best way to display mathematical equations written in HTML within a Flutter

My HTML Text <p>hello</p> <div class="mathjax-equation"> \(x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\) </div> I am interested in displaying this mathematical equation on a Flutter mob ...

Having trouble with the Bootstrap accordion collapse not expanding as expected using CSS

I've been trying to make these collapsibles work, but I'm having no luck. I'm not sure what I'm doing wrong here. I thought it could be my PHP code, but everything seems fine when loading text from the database. Any assistance would be ...

Using the bootstrap CSS file causes my document to shrink in size when I convert it from HTML to PDF using iTextSharp in C# Winforms

I am working on developing a unique PDF export form using HTML and bootstrap that can be utilized in any C# project. My goal is to integrate bootstrap designs into my customized PDF layouts. While my own custom CSS file works perfectly, incorporating the b ...

able to freeze a scrolling element in place on a webpage at its current position

I'm looking for a way to create a one-page website where the initial view is fixed. Each layer that scrolls in from the bottom becomes fixed once it reaches the full position of the viewport, and then the next frame slides in as you scroll. Can this b ...

Reassigning a value in JavaScript can lead to unforeseen outcomes

Within my input element, I currently have the value set as "Go" and the id is #btnSearchBox. I am attempting to replace "Go" with a fontawesome icon. Interestingly, if I manually replace the value "Go" with  (the code for search) and manually add th ...

What steps can be taken to issue an alert when the table does not contain any records?

Upon clicking the submit button, the value from the database is retrieved based on the hidden field ID and displayed in a table. If the value is present, it should load in the table; otherwise, an alert saying 'there is no record' should be displ ...

The positioning of Struts2 datepicker

Can the datepicker display be adjusted to move it slightly to the right? <sj:datepicker name="displayEnd" required="true" key="displayEnd-label" displayFormat="dd-mm-yy" timepicker="true" timepickerStepMinute="15" readonly="true ...

How to use CSS to make a child element's width automatically fill the width of its

Is there a way to make the child div info fill the parent div video-featured width while remaining absolute, allowing it to overlap the parent div without using a fixed width? <div class="video-featured col-md-4"> <div class="video"> < ...

What is the process of generating a tree menu using data from a MySQL database?

Looking to build a dynamic menu tree using PHP and MySQL data. In my MySQL table, I have the following fields: CREATE TABLE IF NOT EXISTS sys_menu ( menu_code varchar(16) COLLATE utf8_unicode_ci NOT NULL, menu_name varchar(64) COLLATE utf8_unicode_ci ...

Does the webworker function as a multi-thread?

For example: worker.postMessage(data1); worker.postMessage(data2); If there are multiple issues to be dealt with inside the web worker, would worker.postMessage(data2) block before completing data1? ...

Rebel Spirit on a Wordpress Platform

I've encountered a strange issue on my Wordpress blog. For some reason, the code "&nbsp" is being inserted right after the opening <body> tag. I have checked the header.php file and there is no instance of this code present there. It's ...

Switch between different stylesheets effortlessly with just one button click

I'm interested in creating a button that can switch between two stylesheets with a single click. Here is the current code I am using: <link id="style1" rel="stylesheet" type="text/css" href="style.css" /> <link id="style2" rel="stylesheet" t ...

Angular 4 application fails to occupy the entire screen height

Here is how my app.component looks: <div class="wrapper"> <app-header></app-header> <router-outlet></router-outlet> <footer> <section class="page group"> {{ 'COMPANY.address' | translate ...

Delete class at waypoints

I am currently using waypoints.js to manage a single-page website that includes a highlighted navigation feature. When the viewport reaches the element with the class "content", the corresponding navigation point is given the class "active". The script i ...

Experiencing difficulty with loading elements into their respective containers

My goal is to limit the number of alert elements loaded in each .content container to a maximum of 9, but currently I am grouping every 10 items together and discarding any remaining items! For instance, if a random value is 8, then no content will be dis ...

What is the best way to adjust the size of an svg with CSS?

Here is a question that may be relevant: How can I resize an SVG? I am interested in resizing the arrow created in SVG using CSS. The linked question achieved this through an HTML directive (viewBox="0 0 32 32") Does anyone have any suggestions? #DIV_ ...

Display and conceal div with Jquery as you scroll to specific points on a mobile device

I'm looking to create a dynamic div that appears and disappears based on the user's scroll position. Here is what I have so far: $(document).scroll(function() { var y = $(this).scrollTop(); if (y > 200) { $('.float-contai ...

Encountering an issue with an Ajax form in the controller of an MVC framework

I am currently enhancing some functionalities on a website created with asp.NET MVC. One of my goals is to display an alert message when data is successfully saved or if an error occurs during the saving process. Below is an overview of my code: --------- ...