JavaScript Window Document property

The Document property in JavaScript is a property of the Window object that refers to the current document in the browser. The Document object represents the HTML document that is loaded in the browser, and provides methods and properties for accessing and manipulating the contents of the document.

Here is an example of how the Document property works:

const myParagraph = document.createElement('p');
myParagraph.textContent = 'Hello, world!';

document.body.appendChild(myParagraph);

In the code example above, we use the document.createElement() method to create a new p element. We then set the textContent property of the p element to the message 'Hello, world!'.

Next, we use the document.body property to access the body element of the document, and the appendChild() method to append the new p element to the body element. As a result, the message 'Hello, world!' will be added to the document as a new paragraph.

The Document object provides a number of useful methods and properties for accessing and manipulating the contents of an HTML document. Some of the most commonly used methods and properties include:

  • document.createElement(): creates a new HTML element
  • document.getElementById(): returns an element with the specified ID
  • document.getElementsByTagName(): returns a list of elements with the specified tag name
  • document.body: returns the body element of the document
  • element.textContent: sets or gets the text content of an element
  • element.appendChild(): appends an element as a child of another element

By using the Document property and its various methods and properties, developers can access and manipulate the contents of an HTML document in the browser. This allows them to create dynamic, interactive web pages that can be updated in real-time based on user input and other events.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: