1var newDiv = document.createElement('div');
2newDiv.innerHTML = '<p>Hello World!</p>';
3document.body.appendChild(newDiv);
1// This is a DOM interface with JAVASCRIPT by using JSON data
2// Creating Table with DOM objects
3
4
5var myData = [{user: "James", address: "123 Keele St. Toronto, ON.", email:"james12@myseneca.ca"},
6 {user: "Mary", address: "92 Appleby Ave. Hamilton, ON.", email:"mary356@myseneca.ca"},
7 {user: "Paul", address: "70 The Pond Rd. North Youk, ON.", email:"paul345@myseneca.ca"},
8 {user: "Catherine", address: "1121 New St. Burlington, ON.", email:"catherine89@myseneca.ca"}];
9
10window.onload = function() {
11 document.querySelector("#button1").onclick = generateTable;
12}
13
14function generateTable(){
15 // get the reference for the body
16 var tbl = document.querySelector("#outputTable");
17
18 // revove existing Body element
19 var tblExistingBody = tbl.querySelector("tbody");
20 if (tblExistingBody) tbl.removeChild(tblExistingBody);
21
22 // creates a <tbody> element
23 var tblBody = document.createElement("tbody");
24
25 // creating all table rows
26 for (var i = 0; i < myData.length; i++) {
27 // creates a table row
28 var row = document.createElement("tr");
29
30 // Create a <td> element and put them at the end of the table row
31 row.appendChild(getTdElement(myData[i].user));
32 row.appendChild(getTdElement(myData[i].address));
33 row.appendChild(getTdLinkElement(myData[i].email, myData[i].email));
34
35 // add the row to the end of the table body
36 tblBody.appendChild(row);
37 }
38
39 // put the <tbody> in the <table>
40 tbl.appendChild(tblBody);
41}
42
43// Create a <td> element and a text
44function getTdElement(text) {
45 var cell = document.createElement("td");
46 var cellText = document.createTextNode(text);
47 cell.appendChild(cellText);
48 return cell;
49}
50
51// Create a <td> element with a hyperlink
52function getTdLinkElement(text, href) {
53 var anchor = document.createElement("a");
54 anchor.setAttribute("href", "mailto:"+href);
55 var anchorText = document.createTextNode(text);
56 anchor.appendChild(anchorText);
57
58 var cell = document.createElement("td");
59 cell.appendChild(anchor);
60 return cell;
61}
62
1<!-- USING EVENT HANDLER FOR DOM HTML -->
2<html lang="en">
3<head>
4 <meta charset="UTF-8" />
5 <title>WEB222</title>
6 <style>
7 body { margin: 0 18%; }
8 </style>
9 <script>
10 window.onload = function() { // why use window.onload?
11 var elem = document.querySelector("#myBtn");
12 elem.addEventListener( "click", displayDate );
13
14 }
15
16 function displayDate() {
17 document.querySelector("#demo").innerHTML = (new Date()).toLocaleString();
18 }
19 </script>
20</head>
21<body>
22 <h1>WEB222 - HTML DOM Event</h1>
23 <p>Click "Show Current Time" to execute the displayDate() function.</p>
24 <p id="demo"></p>
25
26 <button id="myBtn">Show Current Time</button>
27
28 <br>
29 <!-- for downloading source files -->
30 <p><a href="" Download>Download</a></p>
31</body>
32</html>
1// Add new item to start of list
2var newItemFirst = document.createElement('li');
3var newTextFirst = document.createTextNode('New List Item');
4newItemFirst.appendChild(newTextFirst);
5list.insertBefore(newItemFirst, list.firstChild);
1// Add new item to end of list
2var list = document.getElementsByTagName('ul')[0];
3var newItemLast = document.createElement('li');
4var newTextLast = document.createTextNode('Item 2');
5newItemLast.appendChild(newTextLast);
6list.appendChild(newItemLast);