<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dynamic Content Update</title>
</head>
<body>
<p id="textDisplay">Original Text</p>
<button id="updateButton">Change Text</button>
<script src="script.js"></script>
</body>
</html>
document.getElementById('updateButton').addEventListener('click', function() {
const paragraph = document.querySelector('#textDisplay');
paragraph.textContent = 'Updated Text: Hello, World!';
});
const greetings = [
'Hello, World!',
'Hi there!',
'Welcome to our site!',
'Good day!',
'Greetings, traveler!'
];
document.getElementById('updateButton').addEventListener('click', () => {
const randomIndex = Math.floor(Math.random() * greetings.length);
const paragraph = document.querySelector('#textDisplay');
paragraph.textContent = greetings[randomIndex];
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dynamic Style Update</title>
</head>
<body>
<p id="textDisplay" style="color: blue;">Original Text</p>
<button id="updateButton">Change Text and Color</button>
<script src="script.js"></script>
</body>
</html>
document.getElementById('updateButton').addEventListener('click', () => {
const paragraph = document.querySelector('#textDisplay');
const isBlue = paragraph.style.color === 'blue';
paragraph.style.color = isBlue ? 'red' : 'blue';
paragraph.textContent = isBlue ? 'Text is now red!' : 'Text is back to blue!';
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Style Manipulator</title>
</head>
<body>
<ul id="itemList">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<button id="redButton">Red</button>
<button id="greenButton">Green</button>
<button id="blueButton">Blue</button>
<script src="script.js"></script>
</body>
</html>
// Select all the list items
const items = document.querySelectorAll('#itemList li');
// Function to change color of all items
function changeItemColor(color) {
items.forEach(item => {
item.style.backgroundColor = color;
});
}
// Attach event listeners to buttons
document.getElementById('redButton').addEventListener('click', () => changeItemColor('red'));
document.getElementById('greenButton').addEventListener('click', () => changeItemColor('green'));
document.getElementById('blueButton').addEventListener('click', () => changeItemColor('blue'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Attribute Changer</title>
</head>
<body>
<form>
<input type="text" id="inputField" placeholder="Hover over me!">
<button type="submit">Submit</button>
</form>
<script src="script.js"></script>
</body>
</html>
document.addEventListener('DOMContentLoaded', function() {