Selecting DOM Elements
JavaScript provides different methods to select HTML elements from the web page:
getElementById() selects an element using its ID.
getElementsByClassName() selects elements using a class name.
getElementsByTagName() selects elements using tag names.
querySelector() selects the first element that matches a CSS selector.
querySelectorAll() selects all elements that match a CSS selector.
These methods help us access elements to apply changes.
Reading & Changing Content
Once an element is selected, we can read or update its content:
innerText gets or sets only the visible text.
textContent gets or sets all text, including hidden text.
innerHTML gets or sets HTML content inside an element.
This allows us to dynamically update text and HTML on a page.
DOM Attributes & Properties**
DOM allows us to work with element attributes and properties:
getAttribute() reads an attribute value.
setAttribute() adds or updates an attribute.
removeAttribute() removes an attribute.
Properties like id, className, and href allow direct access and modification of element values.
Top comments (0)