I've been working on a small project where I'm using the jQuery .clone()
method.
Pitfall with this is using it on HTML that has unique identifiers.
So I went on implementing getComputedStyle
to find the style properties of the original unique elements, in order to copy it to the clone and give that an new id afterwards (yes, it can give performance issues but it's experimental).
According to the jQuery spec, doing this after cloning but before appending will make the manipulation happen outside of the DOM (so no id 'violation' will occur). But I noticed some strange behaviour across browsers when I try to find style properties of the elements after the object was cloned. Before it, all browsers return the same values but after being cloned :
Firefox - carefree, and interestingly the clone's computed style is the actual CSS value rather than computed data (in pixels).
IE - seems to work but value is not necessarily correct.
Chrome - does not compute. Here's an example :
http://codepen.io/anon/pen/zxqmNK?editors=011
var elements = [];
var objects = [];
$('body').find('[id]').each(function() {
elements.push(this);
});
$('body').clone().find('[id]').each(function() {
objects.push(this);
});
$.each(elements, function(key, element) {
var current = window.getComputedStyle(element, null).getPropertyValue('width');
$('#log').append('<p>' + element.id + ': ' + current + '</p>');
});
$('#log').append('</br>');
$.each(objects, function(count, object) {
var current = window.getComputedStyle(object, null).getPropertyValue('width');
$('#log').append('<p>' + object.id + ': ' + current + '</p>');
});
Anybody know if this is a bug or has similar behaviour been seen before? Not a lot to go on web-wise (not even Stackoverflow). Thanks in advance for any insight.
Edit - did some more testing and it looks like IE behaves the same as Chrome.
Only instead of not returning anything, everything is set to 'auto'.
If the style of the cloned objects is accessed by using .css()
, all values return 0px
(including properties like background).
Seems only Mozilla treats the cloned object as if any style has been applied to it at all.
getComputedStyle
this much? Wouldn't it be easier and way more predictable to style your elements using classes instead of having to find out CSS styles your way? – Ashy