JQuery - copy sizes and absolute position of an element

I am trying to copy the dimensions of an element and the position (relative to the document) to another element.

Example:

  var SelectedElement = $("div#MyTargetElement");

  // The CopiedButEmpty element is a div with absolute position that is meant to hover/float above the selected element.
  $("div#CopiedButEmpty").css("width", SelectedElement.width()).css("height", SelectedElement.height())
    .css("left", SelectedElement.offset().left).css("top", SelectedElement.offset().top)
    .css("marginTop", SelectedElement.css("marginTop")).css("marginLeft", SelectedElement.css("marginLeft"))
    .css("marginRight", SelectedElement.css("marginRight")).css("marginBottom", SelectedElement.css("marginBottom"))
    .css("paddingLeft", SelectedElement.css("paddingLeft")).css("paddingTop", SelectedElement.css("paddingTop"))
    .css("paddingRight", SelectedElement.css("paddingRight")).css("paddingBottom", SelectedElement.css("paddingBottom"));

But in some cases, this still does not give me the right position. Did I miss something? Is there a way to easily copy the position of the element (relative to the document), so my "CopiedButEmpty" element can hover over the element.

0
source share
1 answer

I wonder why you did not choose to use jquery clone () .

+4
source

All Articles