views:

83

answers:

3

I have a dl containing some input boxes that I "clone" with a bit of JavaScript like:

var newBox = document.createElement('dl'); 
var sourceBox = document.getElementById(oldkey); 
newBox.innerHTML = sourceBox.innerHTML; 
newBox.id = newkey;       
document.getElementById('boxes').appendChild(columnBox);

In IE, the form in sourceBox is duplicated in newBox, complete with user-supplied values. In Firefox, last value entered in the orginal sourceBox is not present in newBox. How do I make this "stick?"

+1  A: 

You could try the cloneNode method. It might do a better job of copying the contents. It should also be faster in most cases

var newBox;
var sourceBox = document.getElementById(oldkey);
if (sourceBox.cloneNode) 
    newBox = sourceBox.cloneNode(true);
else {
    newBox = document.createElement(sourceBox.tagName); 
    newBox.innerHTML = sourceBox.innerHTML; 
}
newBox.id = newkey;              
document.getElementById('boxes').appendChild(newBox);
rpetrich
I got this to work by using prototype and changing var sourceBox = document.getElementById(oldkey); to var sourceBox = $(oldkey);
Ross Morrissey
Does document.getElementById not work? I was under the impression that it was cross-browser
rpetrich
A: 

http://stackoverflow.com/questions/36778/firefox-vs-ie-innerhtml-handling#36793 ?

Nickolay
This helps explain things - I used a prototype shortcut to get things working.
Ross Morrissey
+1  A: 

Thanks folks.

I got things to work by using prototype and changing document.getElementById(oldkey) to $(oldkey)

<script src="j/prototype.js" type="text/javascript"></script>  

var newBox;  
var sourceBox = $(oldkey);  
if (sourceBox.cloneNode)  
     newBox = sourceBox.cloneNode(true);  
else {  
    newBox = document.createElement(sourceBox.tagName);  
    newBox.innerHTML = sourceBox.innerHTML;  
}  
newBox.id = newkey;  
document.getElementById('boxes').appendChild(newBox);
Ross Morrissey