Wondering why I can't get document.getElementById("my_div").innerHTML
to update the DOM when I re-assign the variable. For example:
<div id="my_div" onclick="clicky();">
Bye.
</div>
<script type="text/javascript" charset="utf-8">
function clicky() {
var myDivValue = document.getElementById("my_div").innerHTML;
myDivValue = "Hello";
console.log(myDivValue);
}
</script>
In the log I can see the variable get re-assigned when I click, but the innerHTML of my_div remains unchanged. Why is this?
**After several years more experience...**
For those just starting out (like I was) and found yourself asking the same thing, there are two important concepts that need to be understood:
- Assign by reference: My mistake was that I thought
var myDivValue = element.innerHTML
would create a reference/address to innerHTML and every time I assigned a new value to that reference, I could just update content, but that's not how it works. - Assign by value: Instead, I was simply making a copy of
element.innerHTML
value (which was blank) and assigning the value tomyDivValue
, then inmyDivValue = "Hello";
, I was assigning a new value tomyDivValue
, so of course it would never updateelement.innerHTML
.
The confusing part: when using the assignment operator (=
) in JS, it's not explicit that you're either assigning a reference or value ( var referenceName = reference_to_thing
vs. var containerName = newValue
),
As many have said in the answers, the right way to do this is to assign the document.getElementById("my_div")
element to myDiv
:
var myDiv = document.getElementById("my_div")
And now that I have a reference to the element called myDiv
, I can update the innerHTML
property whenever I like:
myDiv.innerHTML = "Hello" // innerHTML is now "Hello"
myDiv.innerHTML = "Goodbye" // Changed it to "Goodbye"
innerHTML
returns a string, not a reference to the DOM. – Poliomyelitisthis
argument. – Poliomyelitisthis
works in this case. I triedconsole.log(document.this)
but it only returns anUndefined.
– Ecosphere<div id="my_div" onclick="clicky(this);">
and thenfunction clicky ( elem ) { elem.innerHTML = ...
– Poliomyelitis