function changepic() { var element, newElement, parent; // Get the original element element = document.getElementById("piccy"); // Assuming it exists... if (element) { // Get its parent parent = element.parentNode; // Create the new element newElement = document.createElement('div'); // Set its ID and content newElement.id = "piccy2"; newElement.innerHTML = "-new content here-"; // Insert the new one in front of the old one (this temporarily // creates an invalid DOM tree [two elements with the same ID], // but that's harmless because we're about to fix that). parent.insertBefore(newElement, element); // Remove the original parent.removeChild(element); } }