Question: What is DOM and what is the purpose of DOM?
Ans:
What is DOM: The full meaning of DOM is Document Object Model. It is a programming interface for web documents. The DOM represents the page so that programs can change the document structure, style and content.
Purpose of DOM: The purpose of DOM is to provide a structure representations of the HTML document as a tree structure where each element of the HTML document is represented as a node in the tree structure. This nodes can be accessed, modified or deleted by using programming languages. The purpose of DOM we can say as follows:
To do access and modify the content and structure of web pages dynamically.
To add, delete or modify HTML and CSS elements on the web.
Respond to user actions like mouse clicks and keyboard inputs.
Animate web page elements and create rich interactive experiences.
Make requests to web servers and receive data in real-time without reloading the page.
Question: How will you select HTML elements using DOM? Name the DOM methods.
Ans: We can select HTML elements using DOM by five ways, which are as follows:
getElementById()
getElementsByTagName()
getElementsByClassName()
quarySelector()
quarySelectorAll()
DOM Methods: The following DOM methods we generally used
document.getElementById() : In this methods we select the element by specific Id.
document.getElementsByClassName() : In this methods we select the element by class name.
document.getElementsByTagName() : In this methods we select the element by tag name.
document.quarySelector() : In this methods we select the element by specific any selector.
document.quarySelectorAll() : In this methods we select the element by specified agroup of selectors.
element.appendChild() : In this methods we append a node as the last child
element.addEventListener() : In this methods we adds an event listener to specified the element
element.setAttribute() : In this methods we sets the value of an attribute .
element.remove() : We use this method to remove the elements from the DOM.
Question: ) What is event bubble?
Ans: Event bubbling is a consept of DOM. It happens when an element receives an event, and that event bubbles up or we can say is transmitted or propagated to its parent and ancestor elements in the DOM tree until it gets to the root element. To stop bubbling we can use event.stopPropagation() methods
Question: What is a callback function and why will you use it?
Ans:
A Callback Function is a function where a function passed into another function as an argument, which is invoked inside the outer function to complete some kind of routin or action.
We use This callback function to allow a function to execute a specific action after it has completed its own processing, often with the result of that processing being passed as an argument to the callback function