HTML DOM Document


The JavaScript DOM document is the root node of the HTML or XML document object model. It represents the entire HTML or XML document as an object, which can be manipulated using JavaScript.

The document object provides a number of properties and methods that allow web developers to access and manipulate various aspects of the document, such as the document's title, URL, and various HTML elements.

For example, the document.getElementById() method can be used to retrieve an element in the HTML document by its unique ID, while the document.getElementsByTagName() method can be used to retrieve a collection of elements in the HTML document with a specified tag name.

Other methods and properties of the document object can be used to dynamically create, modify, and delete HTML elements, manipulate the styles and content of elements, and respond to user interactions and events on the page.


getElementById()

This method returns the HTML element with the specified id attribute.

Example

Preview

getElementsByTagName()

This method returns a collection of all HTML elements with the specified tag name.

Example

Preview

createElement()

This method creates a new HTML element with the specified tag name.

Example

Preview

createTextNode()

The createTextNode() method is used to create a new text node in the HTML DOM. It creates a new text node with the specified text as its content, and returns the newly created text node.

Example

Preview

write()

The document.write() method is used to write content directly into the HTML document at the time it is being parsed.

Example

Preview

addEventListener()

The addEventListener() method is used to attach an event listener to an HTML element. It allows you to specify a function (known as an event handler) that will be executed when a particular event occurs on the element.

Example

Preview

removeEventListener()

The removeEventListener() method is a built-in function in JavaScript that is used to remove an event listener that was previously added to an HTML element using the addEventListener() method.

This method is used when you no longer want the event listener to be triggered for that element. This can be useful when you want to prevent memory leaks or if you want to remove event listeners that are no longer needed.

Example

Preview

open()

The open() method is used to open a new browser window or tab, and the document.write() method is used to write HTML content to the new document.

Example

Preview

close()

Once you have finished writing content to the new document, you can use the close() method to close the document and display it in the new window or tab.

Example

Preview

createAttribute()

The createAttribute() method is used to create a new attribute node with the specified name. This method is part of the Document interface of the DOM (Document Object Model), which represents the HTML or XML document in the browser.

Example

Preview

getElementsByClassName()

The getElementsByClassName() method is a built-in function that is used to select and retrieve a list of HTML elements that have a specific class name. It returns a live HTMLCollection object, which is a collection of all the elements in the current document that match the specified class name.

Example

Preview

getElementsByName()

The getElementsByName() method is used to select and retrieve a list of HTML elements that have a specific name attribute. It returns a live NodeList object, which is a collection of all the elements in the current document that match the specified name attribute.

Example

Preview

querySelector()

The querySelector() method is used to select and retrieve the first element within the current document that matches a specified CSS selector. It returns a reference to the selected element, or null if no matching element is found.

Example

Preview

querySelectorAll()

The querySelectorAll() method is used to select and retrieve all elements within the current document that match a specified CSS selector. It returns a NodeList object, which is a collection of all the elements in the current document that match the specified selector.

Example

Preview

Title

It is a property of the document object that represents the title of the current HTML document.

Example

Preview

Body

The body property is a property of the document object that represents the <body> element of an HTML document. It provides access to the content within the <body> tags of the document.

Example

Preview

Cookie

The document.cookie property is used to read and write cookies associated with the current document.

Example

Preview

URL

The document.URL property returns the entire URL of the current page, including the protocol, domain, port number (if specified), path, and any query parameters. This property can be used to read or change the URL of the current page, which can be useful for various tasks such as redirecting the user to a different page or updating the URL in the address bar.

Example

Preview