DOM in Javascript
What is Document Object Model ?
Document Object model is the web api which manipulates the elements , content of the web page . It is a kind of structure of node or objects which work like a interface in programming languages . A web page is a document contains the html content and the whole body with CSS while Document Object Model is there to manipulate the elements of the document.
DOM And Javascript
Document Object Model is a web api which is not a programming language but it is a independent api which can be used in any programming languages to manipulate DOM . Javascript is nothing without the document object model , because of which the elements can be changed , styled or created and manipulated with the javascript without the html and CSS used. Only with DOM api (web based) javascript is so popular and widely used anywhere with such flexibility
Accessing the DOM elements
You can access the DOM api by internal and external script in the html page and with those scripts you can access window object to access any property of DOM . These are the most common examples of dom properties that are commonly used.
The document can be accessed by the id, class , tag and attribute . This is how you will access the html elements.
Finding html elements :
1. By id : document.getElementById("id")
2. By class : document.getElementsByClass("className")
3. By Tag name : document.getElementsByTagName("p")
3 By All selectors : document.querySelector("#id") , document.querySelector("div") document.querySelector(".className")
Manipulating Html elements:
1. Creating HTML element : document.createElement("div")
2. Removing HTML element : document.removeChild("div")
3. Replacing HTML element : document.replaceChild("new" , "old")
4. Appending HTML element : document.appendChild("p")
4.. Setting attribute : document.setAttribute("attributeName")
5. Writing text to HTML element : document.write("some text")
Changing CSS of HTML elements
1. document.color = "red",
2. document.background = "black"
These are some common DOM methods to manipulate the document and you can explore more once you understand the basic of DOM . Most programmers usually access DOM api with the javascript as it is popular and widely used but it can be used with any programming language because it is independent web browser api . Javascript can be used everywhere with the apis . Nodejs doesn't have access to the DOM api which is a javascript runtime environment.
Thank you for reading the blog post :)
Comments
Post a Comment