You can only call document.write while the page is loading. If you want to dynamically replace text you should create a div element with an ID and use document.getElementById() to access it and replace its content.