1/17/11

Working with DOM , nodes and Objects (part 1) : Adding Nodes

JavaScript is an Object Oriented Programming (OOP) language.

An OOP language allows you to define your own objects and make your own variable types.

Note that an object is just a special kind of data. An object has properties and methods.

The HTML DOM is a W3C standard and it is an abbreviation for the Document Object Model for HTML.


The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate HTML documents.

*----------------------*----------------------*---------------------*


HTML CODE:


<html>
<head>
    <title>Adding Nodes</title>
    <script type="text/javascript" src="script.js">
    </script>
</head>
<body>
    <form action="#">
       <p> 
       <textarea id="textArea" rows="5" cols="30"></textarea> 
       </p>
       <input type="submit" value="Add some text to the page"/>
    </form>
</body>
</html>


script.js :


window.onload = initAll;

function initAll() {
  document.getElementsByTagName("form")[0].onsubmit = addNode;
}

function addNode() {
  var inText = document.getElementById("textArea").value;
  var newText = document.createTextNode(inText);

  var newGraf = document.createElement("p");
  newGraf.appendChild(newText);

  var docBody = document.getElementsByTagName("body")[0];
  docBody.appendChild(newGraf);
    
  return false;
}


It works like this :



 
Before Adding Node (UP)


After Adding A Node.




With this u can Make a Dyanmic Adding of comment into your site,blog etc.

SHARE THIS POST:

Related Posts:

  • Working with DOM,nodes and Objects : Inserting Nodes (part 3) This Post is a continuation of my previous posts ie Adding Nodes and Deleting Nodes. IN this post i am tell how you can add nodes between any two nodes. *----------------------*----------------------*----------------… Read More
  • Roll Overs in HTML Below is a code which helps u create a roll over. for that u need to have a folder (in which the images will be there). Roll overs are like slide show of images (or anything u want), in which u press a button and the image… Read More
  • Detecting a Cookie Here is a piece of code that can help people detect cookies. Cookies play an important part in the website. *****************#####***************** JS Code: window.onload = showCookies; function showCookies() {  &nb… Read More
  • Javascript Smarter Links Here is a piece of code which when you use will make your link in the website SMARTER. This piece of code i used it during my internship and it was well appreciated by the company as it made the user know that the content… Read More
  • Working with DOM , nodes and Objects (part 2) : Deleting Nodes As my last post of DOM was on Adding nodes here is a post that will help you in deleting the last entered node. ie DELETING Nodes. *----------------------*----------------------*---------------------* HTML CODE: … Read More