I wanted to iterate through a DOM cloning a piece of it over and over (for a
series of articles for example), using clone I accomplished this in the
following way:
var articleClone = $('#articleTemplate').clone().get(0);
$(articleClone).removeAttr('id');
var articleCloneHolder = $($(articleClone).clone().get(0));
while (whatever == true) {
$('#indexWrapper').append(articleCloneHolder);
//at the end of the iteration...
articleCloneHolder = $($(articleClone).clone().get(0));
}
#articleTemplate refers to the id of the DOM piece I wanted cloned(in the
html), and the indexWrapper is where each clone would be dropped. It just
loads the DOM template into memory and keeps making clones from the piece in
memory that has no id to speak of. Does anyone else have a better way?
will
Renato Formato wrote:
>
>> Anyway, why a clone() affects the object. Cloning should only create a
>> copy object, not affect the state of the original one.
>>
> I think it is because you may want to chain methods on the cloned object.
>
> If clone() does not substitute the cloned object with the original one,
> you could not call methods on it, as the underlaying object would remain
> the original one.
>
> Anyway clone() does not modify the original DOM element. It just switch
> the DOM element in your jquery object.
>
> Another cause of apparent bug is that you clone an object leaving it
> with the same id of its source. After cloning, you should change the id
> of the copy or you will be unable to select the original object again.
>
> If you want to keep a reference to the original jQuery object, Jorn
> suggestion is perfect.
>
> Renato
>
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
>
>
--
View this message in context:
http://www.nabble.com/clone%28%29-returning-the-cloned-objects-instead-of-a-reference-tf2358771.html#a8121327
Sent from the JQuery mailing list archive at Nabble.com.
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/