HTML (Document Object Model) DOM-java Full
HTML (Document Object Model) DOM-java Full
Now, if the user's browser does not support JavaScript or JavaScript is not enabled,
then the message from </noscript> will be displayed on the screen.
JavaScript - Placement in HTML File
<html
<head>
</head>
<body>
<script>
<!—
document.write("Hello World")
//-->
</script>
<p>This is web page body </p>
</body>
</html>
JavaScript in External File
• Contains only 'A' – 'Z', 'a' – 'z', '0' – '9', '_', '$'
• First character cannot be a digit
• Case-sensitive
• Cannot be reserved words or keywords
JavaScript Variables
• Like many other programming languages,
JavaScript has variables.
• Variables can be thought of as named containers.
• You can place data into these containers and then
refer to the data simply by naming the container.
• Before you use a variable in a JavaScript program,
you must declare it.
• Variables are declared with the var keyword as
follows.
JavaScript Variables
<script>
<!–
var money;
var name;
//-->
</script>
• You can also declare multiple variables with the
same var keyword as follows:
<script>
<!–
var money, name;
//-->
</script
JavaScript Variables
• Storing a value in a variable is called variable initialization. You
can do variable initialization at the time of variable creation or
at a later point in time when you need that variable.
• For instance, you might create a variable named money and
assign the value 2000.50 to it later. For another variable, you
can assign a value at the time of initialization as follows
<script>
<!–
var money=2000.50;
var name=“Ali”;
//-->
</script>
JavaScript Variables
• JavaScript is untyped language.
• This means that a JavaScript variable can hold a
value of any data type.
• Unlike many other languages, you don't have to
tell JavaScript during variable declaration what
type of value the variable will hold.
• The value type of a variable can change during
the execution of a program and JavaScript takes
care of it automatically.
JavaScript Variable Scope
• The scope of a variable is the region of your
program in which it is defined. JavaScript variables
have only two scopes.
• Global Variables − A global variable has global
scope which means it can be defined anywhere in
your JavaScript code.
• Local Variables − A local variable will be visible only
within a function where it is defined. Function
parameters are always local to that function.
JavaScript Variable Scope
• Post/pre increment/decrement
– ++, --
• Comparison operators
– ==, !=, >, >=, <, <=
– ===, !== (Strictly equals and strictly not equals)
• i.e., Type and value of operand must match / must not
match
Logical Operators
• ! – Logical NOT
• && – Logical AND
– OP1 && OP2
– If OP1 is true, expression evaluates to the value of OP2.
Otherwise the expression evaluates to the value of OP1.
– Results may not be a boolean value.
• || – Logical OR
– OP1 || OP2
– If OP1 is true, expression evaluates to the value of OP1.
Otherwise the expression evaluates to the value of OP2.
var tmp1 = null && 1000; // tmp1 is null
• Assignment operators
– =, +=, -=, *=, /=, %=
• Bitwise operators
– &, |, ^, >>, <<, >>>
Operators (continue)
• Miscellaneous Operator
• We will discuss two operators here that are quite useful in
JavaScript: the conditional operator (? :) and the typeof
operator.
• Conditional Operator (? :)
• The conditional operator first evaluates an expression for a
true or false value and then executes one of the two given
statements depending upon the result of the evaluation.
? : (Conditional )
• If Condition is true? Then value X : Otherwise value Y
== vs ===
// Type conversion is performed before comparison
var v1 = ("5" == 5); // true
<script>
document.getElementById("demo").innerHTML = 5;
</script>
</body>
</html>
Using document.write()
• For testing purposes, it is convenient to use
document.write():
• <!DOCTYPE html>
<html>
<body>
<script>
document.write(5 + 6);
</script>
</body>
</html>
alert() and confirm()
alert("Text to be displayed");
• Display a message in a dialog box.
• The dialog box will block the browser.
• 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 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).
Finding HTML Element by Id
• The easiest way to find an HTML element in the DOM, is by using the element id.
• The following example changes the content (the innerHTML) of
the <p> element with id="demo":
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
<p id="demo"></p>
<script>
const element = document.getElementById("intro");
document.getElementById("demo").innerHTML =
"The text from the intro paragraph is: " + element.innerHTML;
</script>
</body>
</html>
Finding HTML Elements by Tag Name
<!DOCTYPE html>
<html>
<body>
<div id="main">
<p>Finding HTML Elements by Tag Name</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method.</p>
</div>
<p id="demo"></p>
<script>
const x = document.getElementById("main");
const y = x.getElementsByTagName("p");
document.getElementById("demo").innerHTML =
'The first paragraph (index 0) inside "main" is: ' + y[0].innerHTML;
</script>
</body>
</html>
Finding HTML Elements by Class Name
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
const x = document.getElementsByClassName("intro");
document.getElementById("demo").innerHTML =
'The first paragraph (index 0) with class="intro" is: ' + x[0].innerHTML;
</script>
</body>
</html>
Finding HTML Elements by HTML Object
Collections
This example finds the form element with id="frm1", in the forms collection, and displays all element values:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
const x = document.forms["frm1"];
let text = "";
for (let i = 0; i < x.length ;i++) {
text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
Changing HTML Content
• The easiest way to modify the content of an HTML element is by using the innerHTML property.
<!DOCTYPE html>
<html>
<body>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
Changing the Value of an Attribute
This example changes the value of the src attribute
of an <img> element:
<!DOCTYPE html>
<html>
<body>
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>
</body>
</html>
Dynamic HTML content
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Date : " + Date();
</script>
</body>
</html>
document.write()
In JavaScript, document.write() can be used to write directly to
the HTML output stream:
<!DOCTYPE html>
<html>
<body>
<script>
document.write(Date());
</script>
</body>
</html>
Never use document.write() after the document is loaded. It will overwrite the document.
JavaScript HTML DOM Events
A JavaScript can be executed when an event occurs, like when a
user clicks on an HTML element.
Examples of HTML events:
• 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
JavaScript HTML DOM Events
In this example, the content of the <h1> element is changed
when a user clicks on it:
<!DOCTYPE html>
<html>
<body>
</body>
</html
HTML Event Attributes
To assign events to HTML elements you can use event attributes.
<!DOCTYPE html>
<html>
<body>
<script>
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
<p id="demo"></p>
</body>
</html>
Assign Events Using the HTML DOM
The HTML DOM allows you to assign events to HTML elements
using JavaScript.
Example : Assign an onclick event to a button element:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("myBtn").onclick = displayDate;
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>
The onmouseover,onmouseout
onmousedown, onmouseup, and onclick
Events
The onmouseover and onmouseout events can be used to trigger a function when the user mouses over,
or out of, an HTML element:
<!DOCTYPE html>
<html>
<body>
<script>
function mDown(obj) {
obj.style.backgroundColor = "#1ec5e5";
obj.innerHTML = "Release Me";
}
function mUp(obj) {
obj.style.backgroundColor="#D94A38";
obj.innerHTML="Thank You";
}
</script>
</body>
</html> uses over, or out of, an HTML element: