DOM in Javascript
What is DOM? And what are its advantages?
DOM stands for Document Object Model. The Document Object Model (DOM) is an API of HTML and XML.
It defines the logical structure of documents and the way a document is accessed and manipulated. In other words, programmers can build documents, navigate their structure, and add, modify, or delete elements and content. Anything found in an HTML or XML document can be accessed, changed, deleted, or added using the Document Object Model
What is DOM in JavaScript?
JavaScript can access, manipulate or modify all the elements in a webpage making use of Document Object Model (DOM).
getElementById, innerHTML Example.
getElementById: To access elements and attributes whose id is set.
innerHTML: To access the content of an element.
getElementsByTagName Example
getElementsByTagName: To access elements and attributes using tag name. This method will return an array of all the items with the same tag name.
Event handler Example
createElement: To create new element
removeChild: Remove an element
You can add an event handler to a particular element like this:
document.getElementById(id).onclick=function()
{
lines of code to be executed
}OR
document.getElementById(id).addEventListener("click", functionname)Try this:
refer guru99