If the X and Y coordinates are given, you can get the element by using document.elementFromPoint(x, y)
. Otherwise, you can get the X and Y by using the method as described in this answer.
So, let X and Y be given. Then:
function appendElementAt(img, x, y){
var elem = document.elementFromPoint(x, y);
var pos = elem.getBoundingClientRect(); //Calculate position of element
var topPos = y - pos.top; // Calculate top position
var leftPos = x - pos.left // Calculate left positon
elem.style.position = "relative";
img.style.position = "absolute";
img.style.top = topPos;
img.style.left = leftPos;
elem.appendChild(img);
}
// Usage:
var img = document.createElement("img");
appendElementAt(img, x, y);