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 elementdocument.getElementById()
: returns an element with the specified IDdocument.getElementsByTagName()
: returns a list of elements with the specified tag namedocument.body
: returns thebody
element of the documentelement.textContent
: sets or gets the text content of an elementelement.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