DEV Community

Syed Muhammad Ali Raza
Syed Muhammad Ali Raza

Posted on

Javascript DOM methods

Mastering JavaScript DOM Methods: Expert Level Implementations and Examples

The Document Object Model (DOM) is a programming interface for web documents. Describe the structure of HTML and XML documents as objects, allowing developers to dynamically manipulate and interact with the content, structure, and style of web pages. In this article, we'll dive deep into some of the most important DOM methods, explore professional-level implementations and provide detailed examples.

Accessing DOM elements

getElementById()

The GetElementById() method is used to retrieve a single element from the DOM by its unique ID attribute. This method offers excellent performance because it goes directly to the element.

Implementation:

const element = document.getElementById('elementID');
Enter fullscreen mode Exit fullscreen mode

Example:

<! DOCTYPE html>
<html>
<head>
  <title>Example getElementById</title>
</head>
<body>
  <div id="myDiv"> Hi, DOM! </div>
  <script>
    const divElement = document.getElementById('myDiv');
    divElement.textContent = 'Hi, I don't know!';
  </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Modify DOM elements

createElement()

The CreateElement() method allows you to dynamically create new HTML elements. This is especially useful if you want to add dynamic elements to the DOM.

Implementation:

const newElement = document.createElement('elementTagName');
Enter fullscreen mode Exit fullscreen mode

Example:

const newParagraph = document.createElement('p');
newParagraf.textContent = 'This is a dynamically generated paragraph.';
document.body.appendChild(new Paragraph);
Enter fullscreen mode Exit fullscreen mode

Set element content

text content

The TextContent property allows you to get or set the text content of the element, including its descendants. It is more efficient and reliable than using "innerHTML" when working with text content.

const content = element.textContent;
Enter fullscreen mode Exit fullscreen mode
element.textContent = 'New Content';
Enter fullscreen mode Exit fullscreen mode

Example:

const paragraph = document.querySelector('p');
const currentText = paragraph.textContent;
paragraph.textContent = 'Updated text content.';
Enter fullscreen mode Exit fullscreen mode

Change element properties

setAttribute()

The setAttribute() method allows you to add or change element attributes. It is especially useful for adding custom attributes or modifying existing ones.

element.setAttribute( 'Attribute name', 'Attribute value');
Enter fullscreen mode Exit fullscreen mode

Example:

const link = document.querySelector('a');
link.setAttribute('href', 'https://www.example.com');
Enter fullscreen mode Exit fullscreen mode

Removing elements

removeChild()

The removeChild() method is used to remove a specific child element from its parent. The basic method is if you want to dynamically remove elements from the DOM.

Implementation:

parentElement.removeChild(childElement);
Enter fullscreen mode Exit fullscreen mode

Example:

const parent = document.querySelector('.parent');
const child = document.querySelector('.child');
parent.removeChild(child);
Enter fullscreen mode Exit fullscreen mode

The results

Mastering DOM manipulation in JavaScript is essential for building dynamic and interactive web applications. By understanding and effectively using these expert-level DOM techniques, you can create simple user experiences and more complex web interfaces. Don't forget to practice these techniques by hand coding to strengthen your experience working with the DOM.

Top comments (3)

Collapse
 
thomasbnt profile image
Thomas Bnt

Hello ! Don't hesitate to put colors on your codeblock like this example for have to have a better understanding of your code 😎

console.log('Hello world!');
Enter fullscreen mode Exit fullscreen mode

Example of how to add colors and syntax in codeblocks

Collapse
 
efpage profile image
Eckehard

The HTML-DOM-API gives you full access to the DOM, but usage is a bit laborious. But there are some libraries like VanJS or DML that try to make DOM handling easiers. If you build your DOM from within Javascript, you get direct access to the DOM, no need to use document.getElementById anymore.

Collapse
 
respect17 profile image
Kudzai Murimi

Well-explained!