html 8
html 8
<script>
const colors = ["#ff9999", "#99ccff", "#ccffcc", "#ffff99", "#d9b3ff"];
let i = 0;
document.getElementById("colorBtn").addEventListener("click", function () {
document.body.style.backgroundColor = colors[i];
i = (i + 1) % colors.length;
});
</script>
</body>
</html>
Output 2:
Program 3:
<!-- Understand and implement onmouseover (hover event) -->
<!DOCTYPE html>
<html>
<head>
<title>Mouse Over - Change Background</title>
</head>
<body>
<button onmouseover="hoverColor()">Hover Over Me</button>
<script>
const hoverColors = ["#e6f7ff", "#ffe6e6", "#e6ffe6", "#fff0b3"];
let hIndex = 0;
function hoverColor() {
document.body.style.backgroundColor = hoverColors[hIndex];
hIndex = (hIndex + 1) % hoverColors.length;
}
</script>
</body>
</html>
Output 3:
Program 4:
<!-- Understand and implement ondblclick (double-click event) -->
<!DOCTYPE html>
<html>
<head>
<title>Double Click - Change Background</title>
</head>
<body>
<button ondblclick="dblColor()">Double Click Me</button>
<bold><p>code by Gautam Nautiyal</p></bold>
<script>
const dblColors = ["#f0f8ff", "#faebd7", "#ffe4e1", "#f5f5dc"];
let dIndex = 0;
function dblColor() {
document.body.style.backgroundColor = dblColors[dIndex];
dIndex = (dIndex + 1) % dblColors.length;
}
</script>
</body>
</html>
Output 4:
Program 5:
<!-- Understand and implement onkeydown (keyboard key press event -->
<!DOCTYPE html>
<html>
<head>
<title>Key Down - Change Background</title>
</head>
<body>
<h3>Press any key to change background color</h3>
<h4>code by Gautam Nautiyal</h4>
<script>
const keyColors = ["#ffe6cc", "#cce6ff", "#e6ccff", "#ccffe6"];
let kIndex = 0;
document.body.onkeydown = function () {
document.body.style.backgroundColor = keyColors[kIndex];
kIndex = (kIndex + 1) % keyColors.length;
};
</script>
</body>
</html>
Output 5:
Program 6:
<!-- Understand and implement onload (when the page finishes loading) -->
<!DOCTYPE html>
<html>
<head>
<title>Page Load - Change Background</title>
</head>
<body>
<h3>Background changes when page loads</h3>
<h4>code by Gautam Nautiyal</h4>
<script>
window.onload = function () {
document.body.style.backgroundColor = "#ccffcc"; // light green
};
</script>
</body>
</html>
Output 6:
Program 7:
<!-- Understand and implement onmousemove (mouse movement over the page) -->
<!DOCTYPE html>
<html>
<head>
<title>Mouse Move - Background</title>
</head>
<body>
<h3>Move your mouse around the page</h3>
<h4>code by Gautam Nautiyal</h4>
<script>
const moveColors = ["#e0ffff", "#f0fff0", "#fff0f5", "#ffffe0"];
let mIndex = 0;
document.body.onmousemove = function () {
document.body.style.backgroundColor = moveColors[mIndex];
mIndex = (mIndex + 1) % moveColors.length;
};
</script>
</body>
</html>
Output 7:
Program 8:
<!-- Understand and implement onchange (dropdown selection change) -->
<!DOCTYPE html>
<html>
<head>
<title>Change Event - Background Color</title>
</head>
<body>
<select onchange="changeColor(this)">
<option value="">-- Select Color --</option>
<option value="#ffc">Light Yellow</option>
<option value="#cff">Light Cyan</option>
<option value="#fcf">Light Magenta</option>
</select>
<h4>code by Gautam Nautiyal</h4>
<script>
function changeColor(selectObj) {
document.body.style.backgroundColor = selectObj.value;
}
</script>
</body>
</html>
Output 8: