faqts : Computers : Programming : Languages : JavaScript : Document

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

68 of 131 people (52%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I use javascript to change text in a document that is already loaded?

May 31st, 2001 06:50
Adam Hardy, Colin Fraser, Eric Sanders, MSDN


This is a quick bit of code that will allow you to add text in IE5+ and 
NS6+. Deleting text can be done using the same family of document 
functions but I haven't included that here.
<HTML>
<HEAD>
</HEAD>
<SCRIPT LANG=Javascript>
<!--
function DoChange() {
   var objHTML, objText;
   objHTML = document.createElement('P');
   objHTML.setAttribute('NAME', 'txtTest');
   objHTML.setAttribute('ID', 'txtTest');
   document.body.appendChild(objHTML);
   var strTest = prompt("Enter any text you want to appear", "whoops!");
   objText = document.createTextNode(strTest);
   objHTML.appendChild(objText);
   delete objHTML;
   delete objText;
}
//-->
</SCRIPT>
<BODY>
   <P>Could be anything!</P>
   <FORM><INPUT TYPE=button ONCLICK=DoChange() VALUE='Try it'></FORM>
</BODY>
</HTML>