Home > Web Front-end > JS Tutorial > body text

My React Journey: Day 5

Linda Hamilton
Release: 2024-11-24 12:44:11
Original
551 people have browsed it

My React Journey: Day 5

Today, I ventured into the world of DOM Manipulation, a fundamental concept in web development. The Document Object Model (DOM) is the bridge between HTML and JavaScript, allowing us to dynamically interact with and modify web pages. Here's what I learned:

What is DOM Manipulation?
DOM Manipulation is the process of using JavaScript to interact with and change the structure, style, or content of a webpage.

Accessing Elements
To manipulate the DOM, we first need to select or access the elements. JavaScript provides several methods to achieve this:

getElementById: Selects an element by its ID.

const header = document.getElementById('main-header');
console.log(header); // Logs the element with ID 'main-header'
Copy after login

querySelector: Selects the first matching element using CSS selectors.

const firstButton = document.querySelector('.btn');
console.log(firstButton); // Logs the first element with class 'btn'
Copy after login

querySelectorAll: Selects all matching elements as a NodeList.

const allButtons = document.querySelectorAll('.btn');
console.log(allButtons); // Logs a list of all elements with class 'btn'
Copy after login

Other Methods:

  • getElementsByClassName (selects elements by class name).
  • getElementsByTagName (selects elements by tag name).

Manipulating Elements
1. Changing Content
Use the innerHTML or textContent property to change the content of an element.

const title = document.getElementById('title');
title.innerHTML = 'Welcome to My React Journey!';
title.textContent = 'Day 5 - DOM Manipulation';
Copy after login

2. Changing Styles
You can dynamically update styles using the style property.

const button = document.querySelector('.btn');
button.style.backgroundColor = 'blue';
button.style.color = 'white';
Copy after login

3. Adding/Removing Classes
Use the classList property to add, remove, or toggle classes.

button.classList.add('active');   // Adds 'active' class
button.classList.remove('btn');  // Removes 'btn' class
button.classList.toggle('hidden'); // Toggles 'hidden' class
Copy after login

4. Attributes
You can modify attributes like src, alt, href, etc.

const image = document.querySelector('img');
image.setAttribute('src', 'new-image.jpg');
image.setAttribute('alt', 'A beautiful scenery');
Copy after login

Event Handling
DOM manipulation often goes hand-in-hand with events. You can listen for user interactions like clicks, keypresses, or mouse movements.

Example: Adding a Click Event

const button = document.querySelector('.btn');
button.addEventListener('click', () => {
    alert('Button clicked!');
});
Copy after login

Example: Updating Content on Input

const input = document.querySelector('#name-input');
input.addEventListener('input', () => {
    const display = document.querySelector('#name-display');
    display.textContent = `Hello, ${input.value}!`;
});
Copy after login

Dynamic Element Creation
You can create and append elements dynamically.

const newElement = document.createElement('p');
newElement.textContent = 'This is a new paragraph added dynamically!';
document.body.appendChild(newElement);
Copy after login

Final Thoughts
DOM Manipulation is incredibly powerful, allowing developers to create interactive and dynamic web pages. It forms the foundation of frameworks like React, where DOM updates are handled more efficiently using virtual DOMs.

I’m excited to see how these concepts play out as I progress further in my React Native journey.

Day 6, here I come! ?

The above is the detailed content of My React Journey: Day 5. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template