Generating HTML On the Fly : HTML Generate « HTML « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » HTML » HTML Generate 
Generating HTML On the Fly

<html>
<head>
<title>Dynamically Generated HTML</title>
<script language="JavaScript">
function initializeDiv() {
 div = document.getElementById("testdiv")
}
function insertElement() {
 var tagIX = document.forms["genform"].elements["elementType"].selectedIndex;
 var tagName = "p";
 if(tagIX == 1
    tagName = "h1";
 else if(tagIX == 2
    tagName = "blockquote";
 else if(tagIX == 3
    tagName = "pre";
    
 var text = document.forms["genform"].elements["ta"].value;
 var newElement = document.createElement(tagName);
 var newText = document.createTextNode(text);
 newElement.appendChild(newText);
 div.appendChild(newElement);
}

function deleteElement() {
 if(div.hasChildNodes()) {
      var children = div.childNodes;
      var n = children.length - 1;
      var lastChild = children.item(n);
      div.removeChild(lastChild);
 }
}
</script>
</head>
<body onload="initializeDiv()">
<h1 align="center">Dynamically Generated HTML</h1>
<hr align="center">
<div id="testdiv">
</div>
<hr align="center">
<form name="genform">
<p>
<b>Tag: </b>
<select name="elementType" size="1">
<option selected="true">P
<option>H1
<option>BLOCKQUOTE
<option>PRE
</select>
<input type="button" value="Insert element" onclick="insertElement()">
<input type="button" value="Delete element" onclick="deleteElement()"></p>
<textarea name="ta" rows="5" cols="40">
Sample text
</textarea>
</form>
</body>
</html>

           
       
Related examples in the same category
1. Using JavaScript to Create HTML Tags
w__ww.__j_a__v_a___2___s___.__c_o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.