Accessing DOM Elements JavaScript
Accessing DOM Elements JavaScript
JavaScript 🌟
Mastering DOM Manipulation in
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
1
Effortlessly Create and Append Elements 🆕
These skills are crucial for web developers working on dynamic and interactive
websites. Keep learning, keep coding! 💪
Accessing Elements in the DOM 3
1. Accessing Elements by ID: 3
2. Accessing Elements by Class Name: 3
3. Accessing Elements by Tag Name: 4
4. Accessing Elements by Selector: 4
5. Accessing Parent and Child Elements: 4
6. Accessing Form Elements: 5
7. Modifying Element Content: 5
8. Modifying Element Attributes: 5
9. Creating New Elements: 5
10. Removing Elements: 6
Coding Exercises 6
Exercise 1: Change Page Title 6
Exercise 2: Change Background Color 6
Exercise 3: Modify Text Content 7
Exercise 4: Create a Button 7
Exercise 5: Remove an Element 7
Exercise 6: Create a List 8
Exercise 7: Modify an Image 8
Exercise 8: Create a Link 8
Exercise 9: Add Event Listener 9
Exercise 10: Create and Append Elements 9
Coding exercise solutions 10
Exercise 1: Change Page Title 10
Exercise 2: Change Background Color 10
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
2
Exercise 3: Modify Text Content 10
Exercise 4: Create a Button 11
Exercise 5: Remove an Element 11
Exercise 6: Create a List 12
Exercise 7: Modify an Image 12
Exercise 8: Create a Link 13
Exercise 9: Add Event Listener 13
Exercise 10: Create and Append Elements 14
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
3
const elements =
document.getElementsByClassName('myClass');
You can access a parent element's children and a child element's parent using the
parentNode and childNodes properties.
const parent =
document.getElementById('parentElement');
const children = parent.childNodes;
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
4
6. Accessing Form Elements:
To access form elements, you can use their name, id, or type attributes.
const usernameInput =
document.forms['myForm'].elements['username'];
You can change the content of an element using the textContent or innerHTML
properties.
To change attributes like src, href, or class, access them and assign new values.
const image = document.getElementById('myImage');
image.src = 'new-image.jpg';
You can create new elements and add them to the DOM using methods like
document.createElement(tagName) and
element.appendChild(newElement).
const newDiv = document.createElement('div');
document.body.appendChild(newDiv);
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
5
10. Removing Elements:
Coding Exercises
Exercise 1: Change Page Title
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
6
1. Access the <div> element using its id.
Description: Create a new button element with the text "Click Me" and add it to
the DOM.
Steps:
1. Create a new <button> element using document.createElement.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
7
Exercise 6: Create a List
Description: Create an unordered list (<ul>) and add three list items (<li>) with
different text to it.
Steps:
1. Create a new <ul> element using document.createElement.
Description: Create an anchor (<a>) element with the text "Visit Google" and a link
to Google.
Steps:
1. Create a new <a> element using document.createElement.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
8
4. Append the <a> element to an existing element in the DOM.
Description: Create a new <div> element, set its class to "box," and append it to
the body of the web page.
Steps:
1. Create a new <div> element using document.createElement.
3. Append the <div> element to the body of the web page using appendChild.
These exercises will help you practice accessing and manipulating elements in the
DOM using JavaScript.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
9
Coding exercise solutions
Exercise 1: Change Page Title
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
10
// Change its textContent property
myParagraph.textContent = 'Hello, JavaScript!';
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
11
Exercise 6: Create a List
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
12
Exercise 8: Create a Link
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
13
Exercise 10: Create and Append Elements
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
14