Solved

Cloning not working

Posted on 2011-03-01
4
392 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

810 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