Solved

Cloning not working

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
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)

636 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