Friday, April 22, 2011

I just learned about jQuery cloning

I was reading jQuery Fundamentals and I learned about $.fn.clone.

I thought, how could I use this? Well at the time I was trying to hold state of an object on a page at a specific time to reuse it later in a certain situation. The problem was, that object was being changed via different events on the page, I wanted a pure version of it. The object happened to be a form. So I cloned it to maintain the initial state:

window.clonedSearchForm = $('#searchForm').clone(true);

I stored the cloned form as a global so I could get it later. True is passed to the clone function as a deep copy. Then using it later:

$(window.clonedSearchForm).submit();

I thought it was cool, likely not.

No comments:

Share on Twitter