0

This is not my exact code, but the relevant parts:

<div id="myDiv" class="class1 class2" style="">
   ... other elements ...
</div>

My jQuery code to clone the element, and then eventually replace the original element with the clone:

clonedDiv = $('#myDiv').clone(true);
... some other code ...
$('#myDiv').replaceWith(clonedDiv);

This works correctly - the only problem is that the cloned div somehow gets a style attribute that was not in the original:

<div id="myDiv" class="class1 class2" 
     style="overflow: hidden; height: 1px; padding-top: 0px; margin-top: 0px; padding-bottom: 0px; margin-bottom: 0px;">
   ... other elements ...
</div>

Where is this coming from...? Is there any way to prevent it? (I am currently "hacking" and explicitly setting it back to an empty string.)

froadie
  • 79,995
  • 75
  • 166
  • 235
  • 1
    Could you show me ur full code? There is no style added when we clone http://jsfiddle.net/uQHx7/6/ – muthu Jan 22 '13 at 10:47
  • it could be some weird side effect of cloning an element that has an id attribute. ids are supposed to be unique and you're breaking that rule by cloning an element with an id attribute. – jakee Jan 22 '13 at 10:51
  • @jakee - I never have 2 elements with identical ids in the DOM, though. I clone it into a variable, then replace the original, so there is only ever 1 element with the id. – froadie Jan 22 '13 at 13:36
  • @muthu - I couldn't really duplicate the error in jsfiddle either... The code is too involved and complicated to post here, I guess I was looking for pointers of what else to look at that may be causing it. And to see if anyone else ever ran into this bug – froadie Jan 22 '13 at 13:37

2 Answers2

1

That might be styled through some javascript or jQuery.

If you want to remove style attribute then do it this way:

clonedDiv = $('#myDiv').removeAttr('style').clone(true);
//---------------------^^^^^^^^^^^^^^^^^^^---------remove the attr then clone it.
Jai
  • 74,255
  • 12
  • 74
  • 103
  • this is essentially what I've been doing as a "hack" as I mentioned in the last line of the post... I was just wondering if there was any way to prevent it from happening in the first place, or at least understand where it's coming from – froadie Jan 22 '13 at 13:39
0

These styles might be the default styles added by your browser. See, all the values are 0px. I don't see how these styles will be creating an issue in the cloned element.

You may check the related post :

Browsers' default CSS stylesheets

Community
  • 1
  • 1
Riju Mahna
  • 6,718
  • 12
  • 52
  • 91