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:

  1. getElementById(): This method allows you to access an HTML element using its id attribute.

  2. getElementsByClassName(): This method returns a collection of HTML elements that have the same class name.

  3. getElementsByTagName(): This method returns a collection of HTML elements that have the specified tag name.

  4. querySelector(): This method returns the first HTML element that matches the specified CSS selector.

  5. querySelectorAll(): This method returns a list of HTML elements that match the specified CSS selector.

  6. innerHTML: This property allows you to get or set the HTML content inside an element.

  7. textContent: This property allows you to get or set the text content inside an element.

  8. setAttribute(): This method allows you to set the value of an attribute on an HTML element.

  9. getAttribute(): This method allows you to get the value of an attribute on an HTML element.

  10. classList: This property returns a list of the classes for an HTML element and provides methods to add, remove, and toggle classes.

  11. style: This property allows you to get or set the style of an HTML element.

  12. parentNode: This property allows you to access the parent node of an HTML element.

  13. childNodes: This property allows you to access the child nodes of an HTML element.

  14. children: This property allows you to access the child elements of an HTML element.

  15. nextSibling: This property allows you to access the next sibling of an HTML element.

  16. 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:

  1. getElementById():

  1. getElementsByClassName():

  1. getElementsByTagName():

  1. getElementsByName():

  1. querySelector():

  1. querySelectorAll():

  1. children:

  1. firstChild:

  1. lastChild:

  1. children: Returns a live HTMLCollection of child elements of the selected element. Example:

  1. firstChild: Returns the first child node of the selected element, or null if the element has no children. Example:

  1. lastChild: Returns the last child node of the selected element, or null if the element has no children. Example:

  1. nextSibling: Returns the next sibling node of the selected element, or null if there is no next sibling. Example:

  1. previousSibling: Returns the previous sibling node of the selected element, or null if there is no previous sibling. Example:

  1. parentNode: Returns the parent node of the selected element, or null if the element has no parent. Example:

  1. parentElement: Returns the parent element node of the selected element, or null if the element has no parent element. Example:

  1. innerHTML:

The innerHTML property is used to get or set the HTML content of an element.

Example:

  1. textContent:

The textContent property is used to get or set the text content of an element.

Example:

  1. style:

The style property is used to get or set the CSS style of an element.

Example:

  1. classList:

The classList property is used to get or set the classes of an element.

Example:

Last updated