createTextNode
method is used to create a text node.
Syntax
document.createTextNode(text);
Example
document.createTextNode("Good
Morning");
function addData(){ var para = document.createElement("p"); var textNode = document.createTextNode("Good Morning"); para.appendChild(textNode); var div = document.getElementById("data1"); div.appendChild(para); }
changeDom.html
<!DOCTYPE html> <html> <head> <title>Change DOM</title> <script src="process.js"></script> </head> <body> <div id="data1"> </div> <input type="button" value="click me" onclick="addData();" /> </div> </body> </html>
Open
changeDom.html in browser, it shows the button ‘cilck me’. For every click of
the button, it adds new paragraph with text "Good Morning" to the
browser window.
No comments:
Post a Comment