PDA

View Full Version : Copy or Move Div blocks while saving their value.



Metsjeesus
Mar 23rd, 2007, 10:05 AM
Well, my idea is to copy contents of one DIV to another while saving all form information. Like user typed values in textboxes, areas and radiobuttons, i need those values in another DIV. If i copy with innerHTML, then this information is lost. So i try DOM nodes. It almost works, in Firefox all elemets go over, exept textarea, IE has problems with radio and checkboxes, they are reset if i copy them.

In DIVs can be multiple forms with same name etc+html structure what i dont know. Example idea of my almost working thing:


<div id="foo">
randomtext
<input type="text" name="test">
<input type="radio" value="0" name="radio">
<input type="radio" value="1" name="radio">
<textarea name="area"></textarea>
etc Form elements
randomtext
</div>
<div id="bar"></div>
<a href="#" onclick="CopyDiv();">Move</a>
<script>
function copyChildNodes(from, to){
to.innerHTML="";
var clone;
for(i=0;i<from.childNodes.length;i++){
clone=from.childNodes[i].cloneNode(true);
to.appendChild(clone);
}
}
function CopyDiv(){
copyChildNodes(document.getElementById('foo'),document.getElementById('bar'));
}
</script>

Kor
Mar 23rd, 2007, 10:50 AM
have you considered cloning the entire DIV, instead of cloning the nested elements one by one?

Metsjeesus
Mar 23rd, 2007, 11:21 AM
Yes, make no difference by me. One thing is, you duplicate DIV ID-s too and must somehow avoid it. Idea is same.