Thursday, December 31, 2020

JavaScript HTML DOM Document

 The HTML DOM document object is the owner of all other objects in your web page.


The HTML DOM Document Object

The document object represents your web page.

If you want to access any element in an HTML page, you always start with accessing the document object.

Below are some examples of how you can use the document object to access and manipulate HTML.


Finding HTML Elements

MethodDescription
document.getElementById(id)Find an element by element id
document.getElementsByTagName(name)Find elements by tag name
document.getElementsByClassName(name)Find elements by class name

Changing HTML Elements

PropertyDescription
element.innerHTML =  new html contentChange the inner HTML of an element
element.attribute = new valueChange the attribute value of an HTML element
element.style.property = new styleChange the style of an HTML element
MethodDescription
element.setAttribute(attribute, value)Change the attribute value of an HTML element

Adding and Deleting Elements

MethodDescription
document.createElement(element)Create an HTML element
document.removeChild(element)Remove an HTML element
document.appendChild(element)Add an HTML element
document.replaceChild(new, old)Replace an HTML element
document.write(text)Write into the HTML output stream

Adding Events Handlers

MethodDescription
document.getElementById(id).onclick = function(){code}Adding event handler code to an onclick event

Finding HTML Objects

The first HTML DOM Level 1 (1998), defined 11 HTML objects, object collections, and properties. These are still valid in HTML5.

Later, in HTML DOM Level 3, more objects, collections, and properties were added.

PropertyDescriptionDOM
document.anchorsReturns all <a> elements that have a name attribute1
document.appletsReturns all <applet> elements (Deprecated in HTML5)1
document.baseURIReturns the absolute base URI of the document3
document.bodyReturns the <body> element1
document.cookieReturns the document's cookie1
document.doctypeReturns the document's doctype3
document.documentElementReturns the <html> element3
document.documentModeReturns the mode used by the browser3
document.documentURIReturns the URI of the document3
document.domainReturns the domain name of the document server1
document.domConfigObsolete. Returns the DOM configuration3
document.embedsReturns all <embed> elements3
document.formsReturns all <form> elements1
document.headReturns the <head> element3
document.imagesReturns all <img> elements1
document.implementationReturns the DOM implementation3
document.inputEncodingReturns the document's encoding (character set)3
document.lastModifiedReturns the date and time the document was updated3
document.linksReturns all <area> and <a> elements that have a href attribute1
document.readyStateReturns the (loading) status of the document3
document.referrerReturns the URI of the referrer (the linking document)1
document.scriptsReturns all <script> elements3
document.strictErrorCheckingReturns if error checking is enforced3
document.titleReturns the <title> element1
document.URLReturns the complete URL of the document1

No comments:

Post a Comment