# Introduction to Document Object Model #### Definition Dom is a tree structure in which every HTML element is arranged in heirarchical order. #### Example ```htmlembedded Document

This is heading 2

This is heading 1

This is Paragraph

``` #### Output ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/258/original/upload_364afb8e43132cd223c90b39c021e52a.png?1695145205) #### DOM tree visualization of above example ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/259/original/upload_604c9f192818c3459c15d376d0569b83.png?1695145257) **Note:** * We will be uisng javascript to put the interactivity in our web apps or websites. * JavaScript is used to put interactivity in DOM elements. #### Question On clicking the button append hello to the page. #### Solution **Step 1:** Slecting html element To select oe identify a particular html element we have methods. * getElementById * QuerySelector * QuerySelectorAll **Code:** Using getElementById ```htmlembedded Document ``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/260/original/upload_a6d7d3c5b18e7b6ed529ca4088ae1dee.png?1695145319) **Code:** Using getElementsByClassName ```htmlembedded Document ``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/261/original/upload_a6d7d3c5b18e7b6ed529ca4088ae1dee_%281%29.png?1695145383) **Code:** Using querySelector by ID ```htmlembedded Document ``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/262/original/upload_7fbdad793912e0939837795b61709f36.png?1695145503) **Code:** Using querySelector by class ```htmlembedded Document ``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/265/original/upload_4718ed7f5720b3cee58f8644ee6a62d2.png?1695145596) **Code:** Using querySelector by elements The document method querySelector() returns the first element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned. ```htmlembedded Document ``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/266/original/upload_a6d7d3c5b18e7b6ed529ca4088ae1dee_%282%29.png?1695145709) **Step 2:** hello should get appended **What is an event?** Anything that happens depending on some other thins is an event. Let's say you are clicking on button then something will happen (hello is getting printed). **Method - addEventListener:** We can add any event to any of our elements with using addEventListener method. ```javascript Document ``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/267/original/upload_c04b7917d2c7dd94fad263612d3bae02.png?1695145825) #### Append Hello DOM Tree ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/270/original/upload_604c9f192818c3459c15d376d0569b83.png?1695145889) #### Question Fix the list by inserting the missing element using querySelectorAll and insertBefore #### Solution **Step 1:** creating node list **Note:** Node list is an array like structure which will have your elements in indexed form stored. ```javascript Document ``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/271/original/upload_fc8a7cf791547f64129ac0eef13aa66e.png?1695146016) **Step 2:** adding element ```javascript Document ``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/273/original/upload_9cda233672dc980dc39259973628c350.png?1695146091) #### Question Fix the mathmatical problem using JS #### Solution ```javascript Document

2 + 2 = 22

``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/274/original/upload_7e599ac46343deed76441a6ec8a415e8.png?1695146150) #### Question Write a script which fetches the data-color attribute of the card and double clicking on them and attahces the fetched class to that card and also changes the data-color attribute to "used" #### Solution **classList:** An element can have multiple classes attached to it and all of those classes are collected in a structure called classList. Example: ```javascript
Our card
``` The classList for this div will be - [card, test, blue] ```javascript Document
``` **Output:** ![](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/049/276/original/upload_108dfa145e3eef14525de6578e2be0f0.png?1695146343)