Module - 3 Javascript Part 2 DOM
Module - 3 Javascript Part 2 DOM
• HTML DOM methods are actions you can perform (on HTML Elements).
• HTML DOM properties are values (of HTML Elements) that you can set or
change.
• The DOM Programming Interface
• The HTML DOM can be accessed with JavaScript (and with other
programming languages).
• In the DOM, all HTML elements are defined as objects.
• The programming interface is the properties and methods of each object.
• A property is a value that you can get or set (like changing the content of
an HTML element).
• A method is an action you can do (like add or deleting an HTML element).
<!DOCTYPE html>
<html>
<body>
<h1>My First Page</h1>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
The getElementById Method
• The most common way to access an HTML element is to use the id of the
element.
• In the example above the getElementById method used id="demo" to find
the element.
• The innerHTML Property
• The easiest way to get the content of an element is by using
the innerHTML property.
• The innerHTML property is useful for getting or replacing the content of
HTML elements.
• The innerHTML property can be used to get or change any HTML element,
including <html> and <body>.
HTML DOM Document
• If you want to find all HTML elements with the same class name, use
getElementsByClassName().
<script>
function myFunction() {
document.getElementById("myH1").style.color = "red";
}
</script>
</body>
</html>
<html>
<head>
<style>
body {
background-color: yellow;
color: red;
font-size: 15px;
}
</style>
</head>
<body>
<p>Click the button to display the style properties of this document.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementsByTagName("STYLE")[0];
document.getElementById("demo").innerHTML = x.innerHTML;
}
</script>
</body>
</html>
<html>
<body>
<p id="myP" style="border-top: 5px solid red;">Click the button to get the
value of my top border.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myP").style.borderTop;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Using Events
• The HTML DOM allows you to execute code when an event occurs.
• Events are generated by the browser when "things happen" to HTML
elements:
• An element is clicked on
• The page has loaded
• Input fields are changed
• This example changes the style of the HTML element with id="id1",
when the user clicks a button:
<body>
<p id="id1">Hello welcome </p>
<button
onclick="document.getElementById('id1').style.color='red'">Click Me!
</button>
</body>
• A JavaScript can be executed when an event occurs, like when a user clicks
on an HTML element.
• To execute code when a user clicks on an element, add JavaScript code to
an HTML event attribute:
• When a user clicks the mouse
• When a web page has loaded
• When an image has been loaded
• When the mouse moves over an element
• When an input field is changed
• When an HTML form is submitted
• When a user strokes a key
• To assign events to HTML elements you can use event attributes.
• Example
• Assign an onclick event to a button element:
• <button onclick="displayDate()">Try it</button>
• The onchange Event
• The onchange event is often used in combination with validation of
input fields.
• Below is an example of how to use the onchange. The upperCase()
function will be called when a user changes the content of an input
field.
• Example
• <input type="text" id="fname" onchange="upperCase()">
<html>
<body>
<p>Select a new car from the list.</p>
<select id="mySelect" onchange="myFunction()">
<option value="Audi">Audi</option>
<option value="BMW">BMW</option>
<option value="Mercedes">Mercedes</option>
<option value="Volvo">Volvo</option>
</select>
<p>When you select a new car, a function is triggered which outputs the value of the selected
car.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>When you leave the input field, a function is triggered which transforms the input text to upper
case.</p>
<script>
function myFunction() {
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
<html>
<body>
<p>Modify the text in the input field, then click outside the field to fire the
onchange event.</p>
Enter some text: <input type="text" name="txt" value="Hello"
onchange="myFunction(this.value)">
<script>
function myFunction(val) {
alert("The input value has changed. The new value is: " + val);
}
</script>
</body>
</html>
<html>
<body>
<script>
function myFunction(elmnt,clr) {
elmnt.style.color = clr;
}
</script>
</body>
</html>
<html>
<body>
</form><br>
<script>
function myFunction() {
var x = document.getElementById("f1");
var sel = document.createElement("select");
sel.id="x";
x.appendChild(sel);
var op=document.createElement("option");
op.text="A";
sel.add(op);
}
</script></body></html>
Window Object
window.alert()
window.confirm()
window.prompt()
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
window.open("https://www.google.com");
}
</script>
</body>
</html>
<html>
<body>
<p>Click the button to open an about:blank page in a new browser window that is
200px wide and 100px tall.</p>
<script>
function myFunction() {
var myWindow = window.open("", "", "width=200,height=100");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>Open a new window, and resize the width and height to 500px:</p>
<button onclick="openWin()">Create window</button>
<button onclick="resizeWin()">Resize window</button>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "", "width=100, height=100");
}
function resizeWin() {
myWindow.resizeTo(250, 250);
myWindow.focus();
}
</script>
</body>
</html>
<html>
<body>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
}
function closeWin() {
myWindow.close();
}
</script>
</body>
</html>
<html>
<body>
<p>Click the first button to alert "Hello" after waiting 3 seconds.</p>
<p>Click the second button to prevent the first function to execute. (You must click it before the 3 seconds are
up.)</p>
<button onclick="myFunction()">Try it</button>
<button onclick="myStopFunction()">Stop the alert</button>
<script>
var myVar;
function myFunction() {
myVar = setTimeout(function(){ alert("Hello"); }, 3000);
}
function myStopFunction() {
clearTimeout(myVar);
}
</script>
</body>
</html>
<html>
<body>
<p>In this example, the setInterval() method executes the setColor() function once every 300 milliseconds,
which will toggle between two background colors.</p>
<script>
var myVar = setInterval(setColor, 300);
function setColor() {
var x = document.body;
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
function stopColor() {
clearInterval(myVar);
}
</script>
</body>
</html>