[jQuery] clone() returning the cloned objects instead of a reference
<div>The clone() function returns the cloned object instead of a copy. Thus, when I modify what it should be the copy, it modifies the original too. This only occurs when I assign the object to clone to a variable and use this variable to call the clone function. For example:
</div>
<div> </div>
<div> <strong>// works as expected</strong></div>
<div> var templateDivCopy = $('#templateDiv').clone().get(0);
templateDivCopy.innerHTML = 'This should be only in the copy';
if(templateDiv.get(0).innerHTML === 'This should be only in the copy') { <strong>// they're different
</strong>
alert('The original was affected too!')
}
</div>
<div> </div>
<div> <strong>// does not work as expected</strong></div>
<div> var templateDiv = $('#templateDiv');
var templateDivCopy = templateDiv.clone().get(0);
templateDivCopy.innerHTML = 'This should be only in the copy'; <strong>//modifies the original too</strong>
if(
templateDiv.get(0).innerHTML === 'This should be only in the copy') { <strong>// they are equal</strong>
alert('The original was affected too!')
}
</div>
<div> </div>
<div>I think this is a bug. </div>
<div> </div>
<div>Is there a fix yet?</div>
<div> </div>
<div>Thanks</div>
<div> </div>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/