esesjay4
asked on
Cloning not working
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)').clon e(true).ht ml().Repla ce("do some operations");
alert($newItemThree);
$('div.my-div:eq(0)).appen d($newItem Three);
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.
var $newItemThree = $('div.my-div:eq(1)').clon
alert($newItemThree);
$('div.my-div:eq(0)).appen
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you using at least jquery 1.4? Older versions didn't always clone events.
ASKER
While you are correct, the regular clone function worked fine. I think it was a problem with the browser cache.
ASKER
$('div.my-div:eq(0)).appen
with:
$('div.my-div:eq(0)).befor
However, cloning the <div> does not seem to cloning the script in IE 8.0 (or 7.0 in fact). Help?