Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can jQuery copy an element's bounds (position, size, margins, etc.) to another element?

I have an element of an arbitrary type. I'd like to create another element, of either the same or a different type that has the same position and size as the first. The element may or may not be positioned.

For example, I might start with a <select> with a certain size, possibly dependent on its contents, i.e. width/height auto. I want to create a new <div> that appears at the same position and has the same size.

I've tried copying the element's float, clear, position, width, height, margins and padding, but this is a little cumbersome. Also, while it works in Firefox, I'm running into some strange issues when testing on Webkit. Before I spend much more time figuring it out, I'd like to know whether there's some jQuery or jQuery UI functionality that already takes care of what I want to do.

I realize that this question is similar to an existing one, but mine has the important distinction of needing to work with elements of differing types, which precludes clone as a solution.

like image 752
DNS Avatar asked Oct 06 '10 18:10

DNS


People also ask

What is clone in jQuery?

jQuery clone() Method The clone() method makes a copy of selected elements, including child nodes, text and attributes.

How append clone in jQuery?

Copy/Paste with clone() & append() using jQuery You can copy an element from one place to another using the clone() function in jQuery. First you make a copy using the clone() function then you use the appendTo() function to indicate where you want the copy placed.

How do you get the position of the bottom of an element in JavaScript?

To set the bottom position of a positioned element with JavaScript, use the bottom property.

How do you find the position of an element in HTML?

Use element. getBoundingClientRect() property to get the position of an element.


2 Answers

This is NOT efficient, tested, or complete. And it is probably similar to what you are already doing. But I thought I'd post it anyways:

var positioningProps = ["float","position","width","height","left","top","marginLeft","marginTop","paddingLeft","paddingTop"];
var select = $("#mySelect");
var div = $("<div>").hide().before(select);
// don't do this kind of loop in production code
// http://www.vervestudios.co/jsbench/
for(var i in positioningProps){
    div.css(positioningProps[i], select.css(positioningProps[i])||"");
}
select.hide();
like image 185
David Murdoch Avatar answered Sep 28 '22 10:09

David Murdoch


How about just copying the element's offset and absolutely positioning it on the page?

Say you had an input element somewhere on the page with dimensions 100x25px.

<input type="text" id="firstname" style="width: 100px; height: 20px" />

And you wanted to place a div right on top of it (and hide the input).

// Store the input in a variable
var $firstname = $("#firstname");

// Create a new div and assign the width/height/top/left properties of the input
var $newdiv = $("<div />").css({
    'width': $firstname.width(),
    'height': $firstname.height(),
    'position': 'absolute',
    'top': $firstname.offset().top,
    'left': $firstname.offset().left
});

// Add the div to the body
$(body).append($newdiv);
like image 40
Marko Avatar answered Sep 28 '22 08:09

Marko