10.module3 DOM-HTML 8
10.module3 DOM-HTML 8
10.module3 DOM-HTML 8
HTML DOM
Node Parents, Children, and Siblings
10
Adding Some Text To Existing <p> (Cont..)
<head>
<script language="javascript" type="text/javascript">
var myText = “ This is new text to be added to the page dynamically.";
function addText(location) {
var newNode, newText, docElement;
newText = document.createTextNode(myText);
docElement = document.getElementById(location);
docElement.appendChild(newText);
}
</script>
</head>
<body>
<p><a href="#" onclick="addText(‘loc’);">Click to add new text to the
page</a></p>
<p id=loc>New text will appear below here</p>
<p>Some further text in the page</p>
</body>
11
Adding <p> and text to a Page (Cont..)
<head>
<script language="javascript" type="text/javascript">
var myText = "This is new text to be added to the page dynamically.";
function addText(location) {
var newNode, newText, docElement;
newNode = document.createElement("p");
newText = document.createTextNode(myText);
newNode.appendChild(newText);
document.body.appendChild(newNode);
}
</script>
</head>
<body>
<p><a href="#" onclick="addText(‘');">Click to add new text to the page</a></p>
<p>New text will appear below here</p>
<p>Some further text in the page</p>
</body>
Add button dynamically
<head>
<script language="javascript" type="text/javascript">
function add(t) {
var element1 = document.createElement("button");
element1.value = t;
element1.name = "b";
element1.setAttribute("style","font-size:50px;background-color:red;");
element1.onclick = function() { alert("new button created"); };
var f1 = document.getElementById("f");
f1.appendChild(element1);
}</script>
</head><body><input type="button" id="btnAdd" value="Add Button"
onClick="add('ADD BUTTON')"><p id="f">Fields:</p>
</body>
13
Remove a Node
14
Remove a Node- Example
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script>
var parent = document.getElementById("div1");
var child = document.getElementById("p1");
parent.removeChild(child);
</script>
15
DOM Events
• HTML DOM Events
• HTML DOM events allow JavaScript to register different
event handlers on elements in an HTML document.
• Events are normally used in combination with functions,
and the function will not be executed before the event
occurs (such as when a user clicks a button).
• When an event occurs, a code segment is executed in
response to a specific event is called “event handler”.
Types of Events
• Keyboard
• Frame
• Drag and drop
• Form
• Clipboard
• Media
• Transition
• Server
• Touch
Event Handlers
18
Event Description DO
M
onmouseout The event occurs when a user moves the mouse pointer out of an 2
element, or out of one of its children
Mouse Events
onmouseup The event occurs when a user releases a mouse button over an element
EventListener
20
EventListener- Example
• Add an event listener that fires when a user resizes the window:
window.addEventListener("resize", function(){
document.getElementById("demo").innerHTML = “hello”;
});
21
XML DOM