Frequently Asked Questions?

1. What is the Document Object Model (DOM)?

The DOM(Document Object Model) is a kind of structure of a webpage. It allows JavaScript to to many work. Such as, changing the content, change or create HTML elements, buttons desing and also allow to add styles and many more.

2. How do you select an element from the DOM?

Well, there is some way to select an element from the DOM.

  • 1. document.getElementById()
  • 2. document.getElementsByClassName()
  • 3. document.getElementsByTagName()
  • 4. document.querySelector()
  • 5. document.querySelectorAll(), etc.

3. What is event delegation in the context of the DOM, and why is it useful?

Event delegation is a JavaScript technique where we can add an event listener to a parent element instead of adding event listener to individual elements. When any event is occurs on a child element's it bubble up to his parent.

4. How do you manipulate an element's attributes and styles using the DOM?

We can manipulate an element's attribute or style by following these methods:

  • 1. element.setAttribute('attribute', 'value')
  • 1. element.removeAttribute('value')
  • 2. element.style.property = 'value'

We can add style to element's using element.style.property = 'value' method.

We can remove attribute from element's using element.removeAttribute('value') method. etc.