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).
Example(1)
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World! Welcome to Bajarangisoft";
</script>
In above example changes the content (the innerHTML
) of the <p>
element with id="demo" and
getElementById
is a method, while innerHTML
is a property.
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.
We can access and change the contents of document by its methods.
The important methods of document object are as follows:
Method | Description |
---|---|
write("string") | writes the given string on the doucment. |
writeln("string") | writes the given string on the doucment with newline character at the end. |
getElementById() | returns the element having the given id value. |
getElementsByName() | returns all the elements having the given name value. |
getElementsByTagName() | returns all the elements having the given tag name. |
getElementsByClassName() | returns all the elements having the given class name. |
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>
.
Complete Code For HTML DOM Function Used In JavaScript
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Function Used In JavaScript</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<br>
<div class="text-center">
<h1>HTML DOM Function Used In JavaScript</h1>
</div>
<br>
<div class="well">
<button class="btn btn-primary" onclick="Function_a()">Click it</button>
<h1 id="demo"></h1>
</div>
<br>
</div>
</body>
</html>
<script>
function Function_a(){
document.getElementById("demo").innerHTML = "Hello World! Welcome to Bajarangisoft";
}
</script>