1. What does DOM stand for in JavaScript?
Answer:
Explanation:
DOM stands for Document Object Model, which is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content.
2. How can you access an element by its ID in the DOM?
Answer:
Explanation:
To access an element by its ID in the DOM, you can use document.getElementById('id') or document.querySelector('#id').
3. Which method is used to create a new element in the DOM?
Answer:
Explanation:
The document.createElement('element') method is used to create a new element in the DOM.
4. How can you add a child element to a parent element in the DOM?
Answer:
Explanation:
The appendChild(child) method is used to add a child element to the end of the list of children of a specified parent element.
5. What does the querySelector method do in the DOM?
Answer:
Explanation:
querySelector returns the first element that matches a specified CSS selector in the document.
6. How do you change the text content of an element in the DOM?
Answer:
Explanation:
The textContent property is used to get or set the text content of an element.
7. Which property is used to access or change the HTML content of an element?
Answer:
Explanation:
The innerHTML property is used to get or set the HTML content of an element.
8. What is the purpose of the addEventListener method in the DOM?
Answer:
Explanation:
The addEventListener method is used to attach an event handler to a specific element.
9. How can you remove an element from the DOM?
Answer:
Explanation:
The remove() method removes the element from the DOM.
10. What does the getElementById method return if no element is found?
Answer:
Explanation:
If no element with the specified ID is found, getElementById returns null.
11. How can you access the style properties of an element in the DOM?
Answer:
Explanation:
The style property is used to get or set the inline style of an element.
12. How do you get all elements with a specific class name in the DOM?
Answer:
Explanation:
The getElementsByClassName method returns a live HTMLCollection of all elements with the specified class name.
13. What is the role of the parentNode property in the DOM?
Answer:
Explanation:
The parentNode property returns the parent node of a specified element in the DOM tree.
14. How can you detect when a user clicks on an element in the DOM?
Answer:
Explanation:
The addEventListener method is used to set up a function that will be called whenever the specified event is delivered to the target.
15. How can you select all <div> elements in the DOM?
Answer:
Explanation:
You can select all <div> elements using document.getElementsByTagName('div') or document.querySelectorAll('div').