I have a div which displays messages sent by users (<div id="messages"></div>). I'm trying to assign a variable 'msg' to the value of the messages in the div but unfortunately the 'msg' doesn't register the value of the new messages added to the div.
As a result, every time I type 'msg' into my console it gives me back "undefined" even though there is a message in the div.
This is what I have so far
var msg;
document.getElementById("#messages").onchange = function () {
msg = this.textContent;
// I soon found out that this code only works for <inputs> and not <div>
}
Here is the message template
<div class="message"> + message.body + </div>
Any suggestions?
The problem is that when the page loads and I send a message into the messages div and then enter 'msg' in my console, I receive "undefined" because the variable hasn't registered that the messages div has changed/updated. So basically I'm looking for a way for the javascript assigning var msg to #messages to reload/update when a new .message is added.