Page 1 of 1

innerHTML store as copy, not reference?

Posted: Mon Aug 07, 2006 11:00 am
by Chris Corbyn
Take the code:

Code: Select all

<script type="text/javascript">
<!-- Hide me from the peeking people

var oldHTML;

function showNewHTML(el)
{
    //I think this is where it breaks
    oldHTML = el.innerHTML;
    el.innerHTML = 'New text';
}

function restoreOldHTML(el)
{
    el.innerHTML = oldHTML; //oldHTML seems to now be 'New text'
}

// -->
</script>
I'm trying to store an element's HTML in a variable, change it temporarily and then restore it again. The HTML seems to be stored by reference though, and so when I change it's value the value of oldHTML changes. Clues or workarounds? :)

Posted: Mon Aug 07, 2006 11:04 am
by feyd

Code: Select all

oldHTML = new String(el.innerHTML);

Posted: Mon Aug 07, 2006 11:25 am
by Chris Corbyn
feyd wrote:

Code: Select all

oldHTML = new String(el.innerHTML);
:oops: How obvious. Thanks buddy.