Add element on top of image dynamically

Felipe Xavier

When I click the button, it has to add an object (img, p) inside the div and the image with border and then take the position of that object, (x, y coordinates).

screenshot

How can I make this using JQuery?

HTML:

<div style="height: 440px; border: 1px solid; width: 880px; margin-top: 50px; margin-left: -12px;">
    <img id="imagem-principal" width="880" height="440" class="custom_media_image attachment-post-thumbnail" src="<?= $segunda_imagem; ?>">
</div>
Jonathan Lam

To have a button create a new <img> and place it on top, add the following styles:

div {
    position: relative;
}
img:not(#imagem-principal) {
    position: absolute;
}

Then, you can add an onclick listener in jQuery:

$("#addImg").click(function() {

    // set x and y to what you want
    var xCoor = // X_COORDINATE;
    var yCoor = // Y_COORDINATE;

    var newImg = $("<img src='/* IMAGE_URL */' />");
    newImg.css({left: xCoor, top: yCoor});
    $("div").append(newImg);
});

This will create a new image with the given x and y coordinates on top of the other image.

See working example on JSFiddle.net.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add element on top of image dynamically

From Dev

How to add an element on top of an image in HTML?

From Dev

Dynamically add element to layout

From Dev

JAXB add element dynamically

From Dev

Dynamically add/remove element

From Dev

Dynamic add element to the top of the page

From Dev

How to add image to top layout

From Dev

Add class to dynamically added element

From Dev

Dynamically add specific attr to element

From Dev

Dynamically add element to XML file

From Dev

Dynamically add element in JS to XUL

From Dev

Dynamically add element using angularJS

From Dev

dynamically add attribute to child element

From Dev

How to dynamically add Image to dynamically created ImageView

From Dev

dynamically add textview on top of imageview android

From Dev

VB - How to dynamically add items on TOP of listview

From Dev

How to dynamically add LinearLayout on top of the TextView

From Dev

Dynamically Add a overlay view on top of a fragment android

From Dev

How to add Image to ImageView dynamically?

From Dev

Dynamically add image to a slider in wordpress

From Dev

Add dynamically a gif image with animation

From Dev

Add an image dynamically to this beautiful frame

From Dev

how to dynamically add image to imageView

From Java

Add overlay image on top of other image

From Dev

Add class when element reaches top of viewport

From Dev

AngularJS: Add a loader element to top of DOM

From Dev

Make element same width as dynamically sized image?

From Dev

Set source of image of dynamically created element in JavaScript

From Dev

How to add an image to an element as a decorator?