DOM Manipulation Hindi | Javascript Hindi Course For Beginners ( 2023 ) #9
When a web page is loaded, the browser creates a Document Object Model of the page.
The HTML DOM is a standard for how to get, change, add, or delete HTML elements.
by Using DOM javascript can do following things
- JavaScript can change all the HTML elements in the page
- JavaScript can change all the HTML attributes in the page
- JavaScript can change all the CSS styles in the page
- JavaScript can remove existing HTML elements and attributes
- JavaScript can add new HTML elements and attributes
- JavaScript can react to all existing HTML events in the page
- JavaScript can create new HTML events in the page
DOM Nodes
Everything in an HTML document is a node
- The entire document is a document node
- Every HTML element is an element node
- The text inside HTML elements are text nodes
- All comments are comment nodes

Navigating Between Nodes
You can use the following node properties to navigate between nodes with JavaScript:
parentNode→ for getting parent nodes
console.log(document.body.parentNode); // This will select parent of body // parent of body is html

childNodes→ for selecting all child node of parents
// Select Child nodes of body console.log(document.body.childNodes);

firstChild→ for getting first child node from parent node
lastChild→ for getting last child node from parent node
nextSibling→ for getting next sibling of current node
previousSibling→ for getting prev sibling of current node
Note:
Selecting a element using nodes is not efficient We can directly select Element nodes
Javascript provides functions to traverse elements nodes we can use them to eliminate text and comment nodes
children → for getting all element nodesfirstElementChild → for getting first element nodeslastElementChild → for getting last element nodesnextElementSibling → for getting next siblings element nodespreviousElementSibling → for getting prev siblings element nodesThere is even better way to select element in DOM elements
getElementById → we can directly select element using its idgetElementByClassName → we can select element using its class namegetElementByName → we can select element using name attributegetElementByTagName → we can select element using tagquerySelector → using specific query we can select elements for selecting element with id we can use → #someId → here we need to append ‘#’ to get element by its id
for selecting element with class we can use → .someClass → here we need to append ‘.’ to get element by its id
This will return only one element
querySelectorAll → its behave same as queryselector but its return multiple elements nodes CRUD of attributes
getAttribute → it will return attribute valuessetAttribute → for setting own attributeremoveAttribute → for removing attributehasAttribute → for checking attribute exist or notNote → correct way of creating custom attributes
“data-coding” use this syntax
using dataset you can access attribute values
More Operations we can perform on element nodes
createElement(”div”) → for creating new element we use this syntaxcreateTextNode(”Hello”) → for creating text element we use this syntaxappend() → for attaching element at end of other element ( inside )prepend() → for attaching element at start of other element ( inside )before() → for attaching element , before other elementafter() → for attaching element , after other elementreplaceWith() → for replacing element with other elementremove() → for removing element classlist.remove(”name”) → removes classclasslist.add(”name”) → add classclasslist.toggle(”name”) → if exist then remove , if not exist then addsinnerHtml() → for adding html inside other elementouterHtml() → for adding html outside Events
When user interacts with website or ui changes with due to some change, this is called events.
examples of events
- The user selects, clicks, or hovers the cursor over a certain element.
- The user chooses a key on the keyboard.
- The user resizes or closes the browser window.
- A web page finishes loading.
- A form is submitted.
- A video is played, paused, or ends.
- An error occurs.
Basic Event example
const btn = document.querySelector("button"); function random(number) { return Math.floor(Math.random() * (number + 1)); } btn.addEventListener("click", () => { const rndCol = `rgb(${random(255)}, ${random(255)}, ${random(255)})`; document.body.style.backgroundColor = rndCol; });
DOM Manipulation Hindi , Javascript Dom Manipulation , DOM JComplete Notes of JS - https://dosomecoding.com/courses/javascript/javascript-hindi-course-for-beginners Github Source Code - https://github.com/anshuopinion/Javascript-Beginners-2023 Javascript Beginners Playlist Link - https://www.youtube.com/playlist?list=PLPppPPmk0i3gZCY8JZ0H5oykFGevvNzNS HTML Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gL2isb9Kr1GvTM8id2gdtY CSS Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gWK5TVILnKSvuc9Fc15sbH Html and CSS practice Projects https://www.youtube.com/playlist?list=PLPppPPmk0i3hZCNmbVtcP1hlwDKOdUFX9 Javascript Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gZCY8JZ0H5oykFGevvNzNS Linkedin - https://linkedin.com/in/anshuopinion Telegram Channel - https://telegram.me/dosomecodinghelp Instagram - https://instagram.com/dosomecoding Github - https://github.com/anshuopinion