<p id="p1">This is paragraph</p>
<!--
สร้าง Element node ที่เหมือนกับ Element Tag นี้
<p>This is a new node</p>
-->
<!--
สร้าง list ที่ เป็นรายชื่อวันในสัปดาห์
<ol>
<li></li>
</ol>
-->
<script>
let days = ["Sunday", "Monday", "Tueday", "Wednesday", "Thursday", "Friday", "Saturday"];
let pElement = document.createElement("p");
// เขียน code ไปพร้อมกัน ^__^
//
// สร้าง list
// 1.Sunday
// 2.Monday
// ........
</script>
<body>
<div id="div1">
<p id="p1">JavaScript</p>
<p id="p2">Python</p>
<p id="p3">TypeScript</p>
<!-- add a new element here -->
</div>
<hr>
<div id="div2">
<p>My favourite fruit.</p>
</div>
<button type="button" onclick="addNode()">Add Node</button>
<button type="button" onclick="deleteNode()">Delete Node</button>
<button type="button" onclick="addNewImage()">Add Image</button>
</body>
<script>
function addNode() {
// <p>a new paragraph</p>
let ptag = document.createElement("p");
// create text node
let txt = document.createTextNode("a new paragraph");
// append text node to a parent node
// select a parent node
let parent1 = document.getElementById("div1");
// append element node to a parent node
/* uncomment in step 2 **/
// let childP1 = document.getElementById("p1");
// parent1.insertBefore(ptag, childP1);
}
function addNewImage() {
// <img src="apples.jpg">
let imgtag = document.createElement("img");
let srcAttr = document.createAttribute("src");
srcAttr.value ="apples.jpg";
imgtag.setAttributeNode(srcAttr);
// method 2
//imgtag.setAttribute('src', 'apples.jpg');
// add to <body>
document.body.appendChild(imgtag);
}
function deleteNode() {
let parent2 = document.getElementById("div1");
let delPtag = document.getElementById("p1");
parent2.removeChild(delPtag);
}
</script>
<head>
<title>DOM Changing color and font-size example</title>
<style>
body { font-Size : 18px;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<p id="pid">Question ? </p>
<button onclick="changeText();">Click me</button>
<div id="ans"></div>
</body>
<script>
function changeText() {
// step 1
let p = document.getElementById("pid");
p.style.color = "blue";
p.style.fontFamily = "Comic Sans MS";
// step 2
// create label
let la = document.createElement("label");
la.innerHTML ="Answer : "
// create input
let inp = document.createElement("input");
let inatt = document.createAttribute("id");
inatt.value = "answerbox";
// add to parent
let parent = document.getElementById("ans");
parent.appendChild(la);
parent.appendChild(inp);
}
</script>
<body>
<table id="table0" border=1>
<tr>
<th>Column 0</th>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</table>
</body>
<script>
let table = document.getElementById('table0');
let row = table.insertRow(-1); //Row position 0,1,2,...,n -1 = last
let cell, text, tnode;
for (let i = 0; i < 3; i++) {
// create a new column
cell = row.insertCell(-1);
text = 'Row ' + row.rowIndex + ', Column ' + i;
tnode = document.createTextNode(text);
// add TextNode to column
cell.appendChild(tnode);
}
</script>