If you just want to replace the text, set the innerText property on the target element. If you want to insert all the HTML, set the innerHTML property on the target element. To replace the target element itself, I believe you can use outerHTML.

basically i want to delete element1 and add element3, innerHTML on "a" will also get rid of element2, and innerHTML on element1 will result in <div class = "element1"><div class = "element3"> some other text</div></div> @Trott
–
Andy ChenOct 15 '12 at 0:06

Given that your problem was, basically, that the 'new content' wasn't a DOM node the most sensible solution is to simply make it a DOM node. Though this does, of course, carry the cross-browser caveats of using document.getElementsByClassName().

And below is a cross-browser version that should provide a fall-back for those browsers that don't support document.getElementsByClassName() (works with IE 7, other versions not available; it's worth noting that the ternary condition may require an upgrade to cope with how some of those browsers might handle the conditional assessment):