What you're doing is storing the DOM Object in the variable, not the text.
You should access the innerHTML
property to access the text.
var t = document.getElementById("foo").innerHTML;
t = t.trim(); // to remove the whitespaces before and after the div.
document.write(t);