Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

DOM Methods

HTML DOM methods are actions you can execute on HTML elements, while HTML DOM properties are values of HTML elements that you can modify or set.

The DOM Programming Interface

The HTML DOM can be accessed using JavaScript (and other programming languages).

In the DOM, all HTML elements are represented as objects.

The programming interface consists of the properties and methods of each object.

A property is a value you can retrieve or modify (such as changing the content of an HTML element).

A method is an action you can perform (like adding or deleting an HTML element).

Example

The following example updates the content (innerHTML) of the <p> element with the id=”demo”:

Example

<html>
<body>

<p id=”demo”></p>

<script>
document.getElementById(“demo”).innerHTML = “Hello World!”;
</script>

</body>
</html>

In the example above, getElementById is a method, and innerHTML is a property.

The getElementById Method

The most common way to access an HTML element is by using its id.

 

In the example above, the getElementById method uses the id=”demo” to locate the element.

The innerHTML Property

The innerHTML property is the simplest way to access the content of an element.

 

It is useful for both retrieving and updating the content of HTML elements.

The innerHTML property can be used to retrieve or modify the content of any HTML element, including <html> and <body>.