So, having my element
<div id="templateDiv">TEMPLATE</div>
If the clone() modifies the jQuery object whe doing
var templateDiv = $('#templateDiv');
var templateDivCopy = templateDiv.clone().get(0); // templateDivCopy is a reference of the original templateDiv
, then why doing
$('#templateDiv').clone().get(0);
doesn't affect the object itself and my changes don't touch the original div?
It looks like an inconsistency to me.
Anyway, why a clone() affects the object. Cloning should only create a copy object, not affect the state of the original one.
Thanks
On 9/29/06, Jörn Zaefferer <[EMAIL PROTECTED]> wrote:
Raziel Alvarez schrieb:
> 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:
>
> *// does not work as expected*
> var templateDiv = $('#templateDiv');
> var templateDivCopy = templateDiv.clone().get(0);
> templateDivCopy.innerHTML = 'This should be only in the copy';
> *//modifies the original too*
> if( templateDiv.get(0).innerHTML === 'This should be only in the
> copy') { *// they are equal*
> alert('The original was affected too!')
> }
>
>
> I think this is a bug.
By calling clone(), the jQuery object saved as templateDiv is modified.
This seems to solve the problem:
var templateDivCopy = $(templateDiv).clone().get(0);
Wrapping the variable templateDiv into the jQuery constructor clones the
jQuery object itself, therefor it can be modified without affecting
templateDiv itself.
-- Jörn
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/
_______________________________________________ jQuery mailing list [email protected] http://jquery.com/discuss/
