Web Programming 2 Weektwonew_034557
Web Programming 2 Weektwonew_034557
By VERNYUY BASILE
TIMELINE/Week 2
Accessing Elements with JS DOM
1. Access 2. Access elements 3. Access elements
elements by ID by tag name by class name
4. Access elements
by CSS selectors
VERNYUY BASILE
Adding and Removing Elements
VERNYUY BASILE
INTRODUCTION
VERNYUY BASILE
Access elements by ID
In JavaScript, you can efficiently select HTML elements by their unique
ID attribute using the getElementById() method. This method returns a
single element object or null if no element with the specified ID exists.
Example
var element = document.getElementById("elementId");
VERNYUY BASILE
Access elements tag name
In JavaScript, you can efficiently select HTML elements based on their
tag name using the getElementsByTagName() method. This method
returns a live HTMLCollection object containing all elements with the
specified tag name within the document.
Example
var elements = document.getElementsByTagName("tagName");
VERNYUY BASILE
Access elements class name
Example
var elements = document.getElementsByClassName("className");
VERNYUY BASILE
Access elements with CSS selectors (querySelector,
querySelectorAll)
In JavaScript, you can efficiently select HTML elements based on their
CSS selectors using the querySelector() and querySelectorAll()
methods. These methods provide a powerful and flexible way to target
elements based on various criteria, such as tag name, class name, ID,
attributes, and more.
1. querySelector():
Returns the first element that matches the specified CSS selector.
If no match is found, returns null.
Example
var element = document.querySelector("cssSelector");
VERNYUY BASILE
Examples
var element = document.querySelector("cssSelector");
In this example:
document.querySelector("#myHeading") selects the element with the
ID "myHeading".
document.querySelector(".important") selects the first element with
the class "important".
VERNYUY BASILE
Examples
VERNYUY BASILE
2. querySelectorAll():
Returns a live HTMLCollection containing all elements that match the
specified CSS selector.
If no match is found, returns an empty HTMLCollection.
Example
var elements = document.querySelectorAll("cssSelector");
Key Points:
i. CSS selectors provide a powerful and flexible way to target
elements based on various criteria.
ii. querySelector() returns the first matching element, while
querySelectorAll() returns a collection of matching elements.
VERNYUY BASILE
Example
VERNYUY BASILE
Append child elements to another element
Example
parentElement.appendChild(childElement);
Explanation:
parentElement: The parent element to which you want to append the
child.
childElement: The child element you want to append.
VERNYUY BASILE
Example
<ul id="myList"></ul>
Example
parentElement.removeChild(childElement);
Explanation:
•parentElement: The parent element from which you want to remove
the child.
•childElement: The child element you want to remove.
VERNYUY BASILE
Example
<ul id="myList">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
list.removeChild(liToRemove);
VERNYUY BASILE
Assignments
Create 4 html elements and access them by Id, tag
name, class name and query selectors. And write js
code to remove and append child elements to the
elements created above. When created add some
dynamic content to all the elements.
VERNYUY BASILE
End