Wrap all your code in onload
function like this:
window.onload = function(){
// Your code;
}
Issue happens because when this line runs :
var textarea = document.getElementsByTagName('textarea')[0];
your textarea is not actually loaded. onload
will solve it.