The easiest way to accomplish this would be to assign an .onchange event handler to the element, then assign the node's value to a variable held outside the scope of the function:
var myMSGvalue;
document.getElementById("MessageBox").onchange = function () {
myMSGvalue = this.value;
// this will work for <input>, not <div>
}
If you really need to assign your data to an html element, you should use an <input type="hidden">.
However, it would be much better to assign the data to a variable before you append it to the page. For example, let's say you have a web socket:
var messages = [];
socket.on("msg", function (msg) {
messages.push(msg);
// msg: { content: "Hello!", author: "notSure" };
functionToAppendToPage(msg);
})
Now, you can see all the messages received in messages and messages[messages.length - 1] will return the last message received (provided at least one message has been received).