JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



DOM Methods

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects. That way, programming languages can interact with the page.

DOM methods allow programmers to access and manipulate the HTML and CSS of a web page. These methods can be used to add, delete, or modify elements on a web page. In this article, we will discuss some of the most commonly used DOM methods.

getElementById()

The getElementById() method is used to get the element with the specified ID. This method returns the element as an object, which can then be manipulated using other DOM methods. Here is an example:


// Get the element with the ID "myElement"
var element = document.getElementById("myElement");

// Change the text of the element
element.innerHTML = "Hello, world!";

getElementsByClassName()

The getElementsByClassName() method is used to get all elements with the specified class name. This method returns an array-like object, which can then be looped through to manipulate each element individually. Here is an example:


// Get all elements with the class name "myClass"
var elements = document.getElementsByClassName("myClass");

// Loop through each element and change the text
for (var i = 0; i < elements.length; i++) {
  elements[i].innerHTML = "Hello, world!";
}

getElementsByTagName()

The getElementsByTagName() method is used to get all elements with the specified tag name. This method returns an array-like object, which can then be looped through to manipulate each element individually. Here is an example:


// Get all elements with the tag name "p"
var elements = document.getElementsByTagName("p");

// Loop through each element and change the text
for (var i = 0; i < elements.length; i++) {
  elements[i].innerHTML = "Hello, world!";
}

createElement()

The createElement() method is used to create a new HTML element. This method returns the new element as an object, which can then be manipulated using other DOM methods. Here is an example:


// Create a new paragraph element
var element = document.createElement("p");

// Set the text of the element
element.innerHTML = "Hello, world!";

// Add the element to the page
document.body.appendChild(element);

appendChild()

The appendChild() method is used to add a new child element to an existing element. This method returns the new element as an object, which can then be manipulated using other DOM methods. Here is an example:


// Get the element to add the new element to
var parent = document.getElementById("myElement");

// Create a new paragraph element
var element = document.createElement("p");

// Set the text of the element
element.innerHTML = "Hello, world!";

// Add the element to the parent element
parent.appendChild(element);

removeChild()

The removeChild() method is used to remove a child element from an existing element. This method returns the removed element as an object, which can then be manipulated using other DOM methods. Here is an example:


// Get the element to remove the child element from
var parent = document.getElementById("myElement");

// Get the child element to remove
var child = document.getElementById("myChild");

// Remove the child element from the parent element
parent.removeChild(child);

setAttribute()

The setAttribute() method is used to set the value of an attribute on an element. This method takes two parameters: the name of the attribute and the value to set. Here is an example:


// Get the element to set the attribute on
var element = document.getElementById("myElement");

// Set the "class" attribute to "myClass"
element.setAttribute("class", "myClass");

getAttribute()

The getAttribute() method is used to get the value of an attribute on an element. This method takes one parameter: the name of the attribute to get. Here is an example:


// Get the element to get the attribute from
var element = document.getElementById("myElement");

// Get the value of the "class" attribute
var value = element.getAttribute("class");

These are just a few of the many DOM methods available to web developers. By using these methods, developers can create dynamic and interactive web pages that respond to user input and change over time.

References

Activity