Thursday 28 February 2013

Read from and write to HTML document


Since JavaScript has become an integrated part of the Frond End Development, you must learn how to read from and write to an HTML document.
Before we go the actual coding, let us have a brief discussion on DOM - Document Object Model, because that will help you to understand the topic better.

A brief note on DOM

Document Object Model is an interface that allows scripts and programs to dynamically access and update content(e.g. text, image etc.), structure and style of HTML, XHTML and XML documents. For this discussion, we will focus on HTML documents only. Document Object model is a W3C recommendation.
Let's take a simple HTML page like following:
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset=utf-8>  
  5. <title>A simple HTML document</title>  
  6. </head>  
  7. <body>  
  8. <h1>This is a simple HTML document</h1>  
  9. <p>w3resource web development tutorial</p>  
  10. </body>  
  11. </html>  
Now, DOM representation of the above code is following:
a simple DOM
All those blue boxes are called as Nodes.

Write data to HTML document

Now, we will see how you may write some text in an HTML document. The following JavaScript code may be used to create a new paragraph in an HTML document and add some text within that.
  1. var w3r_text = "This text will be added to HTML";  
  2. var newParagraph = document.createElement("p"); //creates a new paragraph element  
  3. var newText = document.createTextNode(w3r_text); //creates text along with output to be displayed   
  4. newParagraph.appendChild(newText); //created text is appended to the paragraph element created  
  5. document.body.appendChild(newParagraph); // created paragraph and text along with output is appended to the document body  
We have added comments next to each line of code, so that you can understand it. You may use our comments section to let us know if you have questions. If the code above is added to the HTML code we have begin with, it bo comes:
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset=utf-8>  
  5. <title>A simple HTML document</title>  
  6. </head>  
  7. <body>  
  8. <h1>This is a simple HTML document</h1>  
  9. <p>w3resource web development tutorial</p>  
  10. <script src="add_text.js"></script>  
  11. </body>  
  12. </html>  
Did you see the new text in the HTML output now? So, that is how you may write to an HTML document.

Read data from HTML document

Want to read data from HTML document? even easier. Let's see how you may do that. This is the JavaScript code we will use for this example
  1. var shdata = document.getElementById('shtxt').innerHTML;  
  2. alert(shdata);  
So, in the first line, we are collecting the text within the paragraph, whose id is - shtxt. 'document' refers to the document where the script is being run, 'getElementById' selects the element whose id is 'shtxt' and innerHTML selects the actual text. We store that value in the variable 'shdata' and then, by using alert command, the text is displayed.
Here is the HTML code where the JavaScript above is added:
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4. <meta charset=utf-8>  
  5. <title>A simple HTML document</title>  
  6. </head>  
  7. <body>  
  8. <h1>This is a simple HTML document</h1>  
  9. <p id="shtxt">w3resource web development tutorial</p>  
  10. <p><a href="#" onclick="Showdata();">Show text</a></p>  
  11. <script src="fetch_text.js"></script>  
  12. </body>  
  13. </html>  

No comments:

Post a Comment