Accessing Elements
In this chapter we will learn most commonly used properties and methods to accessing element of DOM . There are several properties and methods you can use to access and manipulate HTML elements in JavaScript. Here is a list of some commonly used ones:
getElementById(): This method allows you to access an HTML element using itsidattribute.getElementsByClassName(): This method returns a collection of HTML elements that have the same class name.getElementsByTagName(): This method returns a collection of HTML elements that have the specified tag name.querySelector(): This method returns the first HTML element that matches the specified CSS selector.querySelectorAll(): This method returns a list of HTML elements that match the specified CSS selector.innerHTML: This property allows you to get or set the HTML content inside an element.textContent: This property allows you to get or set the text content inside an element.setAttribute(): This method allows you to set the value of an attribute on an HTML element.getAttribute(): This method allows you to get the value of an attribute on an HTML element.classList: This property returns a list of the classes for an HTML element and provides methods to add, remove, and toggle classes.style: This property allows you to get or set the style of an HTML element.parentNode: This property allows you to access the parent node of an HTML element.childNodes: This property allows you to access the child nodes of an HTML element.children: This property allows you to access the child elements of an HTML element.nextSibling: This property allows you to access the next sibling of an HTML element.previousSibling: This property allows you to access the previous sibling of an HTML element.
Now, here are examples for each of the properties related to accessing elements in JavaScript:
getElementById():
getElementsByClassName():
getElementsByTagName():
getElementsByName():
querySelector():
querySelectorAll():
children:
firstChild:
lastChild:
children: Returns a live HTMLCollection of child elements of the selected element. Example:
firstChild: Returns the first child node of the selected element, ornullif the element has no children. Example:
lastChild: Returns the last child node of the selected element, ornullif the element has no children. Example:
nextSibling: Returns the next sibling node of the selected element, ornullif there is no next sibling. Example:
previousSibling: Returns the previous sibling node of the selected element, ornullif there is no previous sibling. Example:
parentNode: Returns the parent node of the selected element, ornullif the element has no parent. Example:
parentElement: Returns the parent element node of the selected element, ornullif the element has no parent element. Example:
innerHTML:
The innerHTML property is used to get or set the HTML content of an element.
Example:
textContent:
The textContent property is used to get or set the text content of an element.
Example:
style:
The style property is used to get or set the CSS style of an element.
Example:
classList:
The classList property is used to get or set the classes of an element.
Example:
Last updated