In this lesson, you'll explore scripting with JavaScript and how to manipulate the Document Object Model (DOM). The DOM represents the structure of your HTML document, allowing JavaScript to change content, styles, and more dynamically.
You'll set up a new project in VS Code, create an HTML page, add CSS for styling, and use JavaScript to add interactivity. By the end, you'll use onclick attributes, event listeners, and manipulate elements like text colour and size.
Make sure you have VS Code installed from the previous lesson.
First, create a new workspace for this lesson in VS Code.
Open VS Code and follow these steps:
File
' > 'Open Folder
'.DOMManipulation
' in your Documents folder.Now, create the basic HTML structure for your page.
In VS Code, with your project folder open:
New File
'.index.html
'.Add the following complete HTML code to 'index.html
':
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOM Manipulation Demo</title>
</head>
<body>
<h1>Welcome to DOM Manipulation</h1>
<p id="demoText">This is some text we'll manipulate with JavaScript.</p>
<button>Change Text</button>
</body>
</html>
Save the file (Ctrl+S
or Cmd+S
). This sets up a simple page with a heading, paragraph, and button.
To view it, double-click 'index.html
' in your file explorer to open it in a web browser.
To style your page, create a CSS folder and file.
In VS Code:
New Folder
'.css
'.css
' folder and select 'New File
'.styles.css
'.Add the following complete CSS code to 'styles.css
':
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
text-align: center;
}
h1 {
color: navy;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
Now, link the CSS to your HTML. Update 'index.html
' with this complete code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOM Manipulation Demo</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>Welcome to DOM Manipulation</h1>
<p id="demoText">This is some text we'll manipulate with JavaScript.</p>
<button>Change Text</button>
</body>
</html>
Next, create a folder for JavaScript and add a script file.
In VS Code:
New Folder
'.js
'.js
' folder and select 'New File
'.script.js
'.For now, leave 'script.js
' empty. We'll add code soon.
Link the JavaScript to your HTML. Update 'index.html
' by adding in the new script
tag at the end of the body tag:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DOM Manipulation Demo</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>Welcome to DOM Manipulation</h1>
<p id="demoText">This is some text we'll manipulate with JavaScript.</p>
<button>Change Text</button>
<script src="js/script.js"></script>
</body>
</html>