Solved

Cloning not working

Posted on 2011-03-01
4
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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…

735 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