innerHTML store as copy, not reference?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

innerHTML store as copy, not reference?

Post 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? :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

oldHTML = new String(el.innerHTML);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:

Code: Select all

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