-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript-guide.txt
More file actions
38 lines (22 loc) · 1.11 KB
/
javascript-guide.txt
File metadata and controls
38 lines (22 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
JavaScript Guide: Adding Interactivity
Objective: Add JavaScript to make the webpage interactive. This guide covers basic event listeners and a dark mode toggle.
JavaScript Basics
Add a click event listener to a button to show an alert.
document.getElementById('jsButton').addEventListener('click', function(){
alert('Congratulations! Your JavaScript Works!!!');
});
Dark Mode Toggle
Add an event listener to a button to toggle dark mode.
const toggleDarkModeButton = document.getElementById('jsToggleBtn');
toggleDarkModeButton.addEventListener('click', function(){
document.body.classList.toggle('dark-mode');
document.querySelectorAll('section').forEach(section => {
section.classList.toggle('dark-mode');
});
document.querySelector('header').classList.toggle('dark-mode');
document.querySelector('footer').classList.toggle('dark-mode');
});
Optional DOM Manipulation
Change the text and style of an element (uncomment to use).
// document.getElementById('domTest').innerText = 'Hello';
// document.getElementById('domTest').style.color = 'red';