DOM(Document object model) and selectors in javascript

Basic of DOM

DOM stands for (document object model) is used as a structure representation of HTML.It is a platform to combine the program of dynamic sites so that you can access the data and their content.It gives the structure of your site and and new design to your document.It represent the document which is based on node and object so that programming language can work as a interface for the page.The DOM is object oriented which means each node has their own property and their methods so that we can make changes in DOM.

DOM selectors

DOM selectors is used to bring basic things from DOM and you can use in many different ways with nodes and elements .Since jquery is used for long time for handling and controlling the DOM selectionbut with the help of java script it is very easy to work .Besides these jquery is very helpful for simple plugins and script that can used for simple actions .In javascript there are many selectors which are categories in two different part:

  1. Single Element selectors 
  2. Multiple Element selectors

Single Element selectors 

Single Element selectors allow to catch single element by its ID or CLASS whatever you define in your DOM  and it only store one thing so when you use a single element selector and use class it show once in a DOM,it just catch the first element.


//document.get element by ID

This code gives you the code whose ID is test1. So basically it is used to find the code if you know the ID.

//Get things from element
So by using this code you will know the CLASSNAME and ID.
//change style
document.getElementById("test1").style.background="#A52A2A"; document.getElementById("test1").style.paddingTop="5px";

//change content
document.getElementById("test1").textContent="Apple mobile";     
document.getElementById("test2").innerHTML='<span style="color:DarkMagenta "> Puma shoes</span>';
//use can define variable and use that variable instead of writing document.getElementById so many times
const test=document.getElementById('test1');
test.innerHTML='<span style="color:DarkMagenta "> Puma shoes</span>';

Query selectors

//Query selectors

In the single selector  first element is change its not apply to all the element with same CLASSNAME or ID  name so for that we use different method.

//Query selectors()
//document.querySelector('ul li').style.color='DarkSlateBlue';
document.querySelector('li:nth-child(3)').textContent='Titan Watch';


Multiple Element selectors

Multiple Element selectors will give all of the element with their class and then return in DOM in the node which you like to use.It is also helpful to  convert in the array form .

const test=document.getElementsByClassName('list-group-item');
test[3].textContent='BMW car';

By using  console.log(test);  it show the list-item of a given class and if you want to see single itemof a particular class then you use console.log(test[0]); .

If you want to change style of particular item than use the code given above.

Using Tags to Get Elements

With the help of tags you change style in javascript  it work same as you defineID or CLASS ,the only difference is that we have to define particular tag.

//Using Tagsname to get elements
const list=document.getElementsByTagName('li');
list[3].textContent='BMW car';

You get the same output same as above by using tagsname also.The inner work is same