Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Dom Document in JavaScript

Dom Document in JavaScript

? DOM Document in JavaScript


What is the Document Object?

The document is the root of the DOM (Document Object Model) in a web page. It represents the entire HTML document and provides methods and properties to access, modify, and manipulate the page’s content.


Key Features of document

  • Access elements (getElementById, querySelector, etc.)

  • Create new elements (createElement)

  • Listen to events on the page

  • Get information about the page (title, URL, etc.)

  • Modify the HTML structure dynamically


Commonly Used document Methods and Properties

Method / PropertyDescriptionExample
document.getElementById(id)Get element by IDdocument.getElementById("header")
document.getElementsByClassName(className)Get elements by class namedocument.getElementsByClassName("item")
document.querySelector(selector)Get first element matching CSS selectordocument.querySelector(".btn")
document.querySelectorAll(selector)Get all elements matching selectordocument.querySelectorAll("p")
document.createElement(tagName)Create a new elementdocument.createElement("div")
document.bodyGet the <body> elementdocument.body.style.background = "yellow"
document.titleGet or set the page titledocument.title = "New Title"
document.URLGet the current page URLconsole.log(document.URL)
document.addEventListener(event, handler)Attach event handler to the documentdocument.addEventListener("click", () => alert("Clicked!"))

Example: Change the Page Title

console.log(document.title);  // Current titledocument.title = "My New Page Title";  // Change title dynamically

Example: Create and Insert a New Element

const newDiv = document.createElement("div");newDiv.textContent = "Hello, new div!";document.body.appendChild(newDiv);

Example: Selecting and Changing Element Content

<p id="para">Original Text</p><script>const para = document.getElementById("para");para.textContent = "Text changed with JavaScript!";</script>

Summary

  • document is your gateway to interact with the entire webpage.

  • You can select, create, modify, and listen to page elements using document.

  • It's the foundational object for DOM manipulation in JavaScript.


Would you like to see examples on event handling with document, or how to traverse the DOM tree?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql