---
# System prepended metadata

title: DOM Methods  and Events in JavaScript

---

**Understanding DOM Methods  and Events in JavaScript**

JavaScript enables developers to create dynamic and interactive web applications. A vital aspect of this interactivity comes from the manipulation of the Document Object Model (DOM) and handling events. This article explains essential DOM events and methods that are foundational to modern web development.

---

### DOM Methods

The DOM provides many methods to interact with and manipulate elements on a web page. Here are some of the most commonly used DOM methods:

#### 1. **createElement**
Creates a new element of the specified type.
```javascript
const div = document.createElement('div');
```

#### 2. **appendChild**
Adds a new child node to an existing node.
```javascript
document.body.appendChild(div);
```

#### 3. **querySelector**
Returns the first element that matches a CSS selector.
```javascript
const firstButton = document.querySelector('button');
```

#### 4. **querySelectorAll**
Returns a NodeList of all elements matching a CSS selector.
```javascript
const allButtons = document.querySelectorAll('button');
```

#### 5. **getElementById**
Returns the element with the specified ID.
```javascript
const header = document.getElementById('main-header');
```

#### 6. **getElementsByClassName**
Returns a live HTMLCollection of elements with a given class name.
```javascript
const boxes = document.getElementsByClassName('box');
```

#### 7. **getElementsByTagName**
Returns an HTMLCollection of elements with the specified tag name.
```javascript
const paragraphs = document.getElementsByTagName('p');
```

#### 8. **getElementsByName**
Returns a NodeList of elements with a given name attribute.
```javascript
const inputs = document.getElementsByName('username');
```

#### 9. **setAttribute**
Sets the value of an attribute on the specified element.
```javascript
div.setAttribute('id', 'newDiv');
```

#### 10. **getAttribute**
Gets the value of a specified attribute.
```javascript
const id = div.getAttribute('id');
```

#### 11. **classList.add**
Adds one or more class names to an element.
```javascript
div.classList.add('highlight');
```

#### 12. **classList.remove**
Removes a class name from an element.
```javascript
div.classList.remove('highlight');
```

#### 13. **classList.toggle**
Toggles a class name on or off.
```javascript
div.classList.toggle('hidden');
```

#### 14. **classList.replace**
Replaces an existing class name with a new one.
```javascript
div.classList.replace('old-class', 'new-class');
```

#### 15. **removeChild**
Removes a child element from its parent.
```javascript
const parent = document.getElementById('container');
const child = document.getElementById('item');
parent.removeChild(child);
```

#### 16. **remove**
Removes the element from the DOM entirely.
```javascript
div.remove();
```

#### 17. **style.{cssProperty}**
Directly sets CSS styles on elements.
```javascript
div.style.color = 'red';
```

#### 18. **parentElement**
Returns the parent element of a specified element.
```javascript
const parent = child.parentElement;
```

#### 19. **nextElementSibling**
Returns the next sibling element in the DOM.
```javascript
const next = child.nextElementSibling;
```

---

### Working with Dates

JavaScript’s `Date` object allows you to handle dates and times easily.
```javascript
const now = new Date();
console.log(now.toString());
```
You can retrieve specific parts of the date using methods like:
```javascript
const year = now.getFullYear();
const month = now.getMonth() + 1; // Months are zero-based
const day = now.getDate();
```



### DOM Events

DOM events are actions or occurrences that happen in the browser, which you can respond to using JavaScript. Here are some key DOM events:

#### 1. **DOMContentLoaded**
The `DOMContentLoaded` event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

```javascript
window.addEventListener('DOMContentLoaded', () => {
  console.log('DOM fully loaded and parsed');
});
```

This is often used to run scripts after the structure of the page is ready but before all resources have loaded.

#### 2. **load**
The `load` event fires when the whole page, including all dependent resources like stylesheets and images, has fully loaded.

```javascript
window.addEventListener('load', () => {
  console.log('Page fully loaded');
});
```

#### 3. **beforeunload**
The `beforeunload` event is triggered when the user attempts to leave the page — by closing the tab, navigating to a different site, or refreshing the page. You can use this to display a confirmation dialog.

```javascript
window.addEventListener('beforeunload', (event) => {
  event.preventDefault();
  event.returnValue = '';
});
```

#### 4. **click**
The `click` event fires when a user clicks on an element. This is one of the most frequently used events.

```javascript
document.getElementById('myButton').addEventListener('click', () => {
  alert('Button was clicked!');
});
```

---

---

### Conclusion

Mastering DOM events and methods is crucial for building interactive and dynamic web applications. These tools allow you to listen for user interactions, manipulate elements, and control page behavior with precision. By practicing these concepts, you will gain the confidence and ability to create responsive and engaging user experiences on the web.

---

Keep exploring and experimenting with the DOM—it’s your gateway to building truly dynamic websites!

