Solved

Cloning not working

Posted on 2011-03-01
4
388 Views
Last Modified: 2012-06-21
I have an html DOM similar to the one below.  The <script> section has code and the <table> section has table data.  I try to clone the <div> section item, render it as html, operate on the raw string and re-insert the element between item 1 and 2.  Here's some sample code of what I'm doing:

var $newItemThree = $('div.my-div:eq(1)').clone(true).html().Replace("do some operations");

alert($newItemThree);

$('div.my-div:eq(0)).append($newItemThree);


Now, the alert displays the proper script and table elements, but is missing the outer div (missing <div class=my-div">).  After the append() call, the rendered DOM object is missing the <script> information as well as the outer div.

What gives?  I've even tried .clone() (without true) and it still doesn't work.

FYI, I HAVE to convert to html() in order to do some manual string manipulation to data.  So that has to stay there.
**********  ORIGINAL ************

<div class="my-div">
     <script>...</script>
     <table>...</table>
</div>
<div class="my-div">
     <script>...</script>
     <table>...</table>
</div>
<div class="my-div">
     <script>...</script>
     <table>...</table>
</div>

*****************  AFTER CLONE & INSERT ************

<div class="my-div">
     <script>...</script>
     <table>...</table>
</div>
<table>...</table>
<div class="my-div">
     <script>...</script>
     <table>...</table>
</div>
<div class="my-div">
     <script>...</script>
     <table>...</table>
</div>

Open in new window

0
Comment
Question by:esesjay4
  • 2
4 Comments
 

Author Comment

by:esesjay4
ID: 35020429
Fixed the append issue by replacing:

$('div.my-div:eq(0)).append($newItemThree);

with:

$('div.my-div:eq(0)).before($newItemThree);

However, cloning the <div> does not seem to cloning the script in IE 8.0 (or 7.0 in fact).  Help?
0
 
LVL 15

Accepted Solution

by:
SRigney earned 250 total points
ID: 35031124
Script is not part of the dom like that for it to clone within that div.  It does appear in the html there, but not actually in the dom there.

I'd probably try figuring out how to make the script work by only being inserted once after all the divs.  My guess is that with .live or .delegate you could setup the method to work on all of the objects correctly.

Also even with text manipulation I doubt you have to convert it to html.  When you clone the object you get a jquery reference to an element that is not in the dom.  You can use selectors to manipulate text and value attributes of the object without it being converted to html.
0
 
LVL 4

Expert Comment

by:Tomun
ID: 35038076
Are you using at least jquery 1.4? Older versions didn't always clone events.
0
 

Author Closing Comment

by:esesjay4
ID: 35114751
While you are correct, the regular clone function worked fine.  I think it was a problem with the browser cache.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now