How JavaScript Interacts with HTML & CSS ππ₯οΈ

How JavaScript Interacts with HTML & CSS ππ₯οΈ
JavaScript is the glue that connects HTML (structure) and CSS (styling), allowing dynamic and interactive web pages.
1οΈβ£ JavaScript + HTML: Manipulating Content
JavaScript can add, remove, or modify HTML elements dynamically using the DOM (Document Object Model).
β
Example: Change Text Dynamically
<!DOCTYPE html>
<html lang="en">
<head>
<title>JS + HTML</title>
</head>
<body>
<h1 id="heading">Hello, World!</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("heading").innerText = "Hello, JavaScript!";
}
</script>
</body>
</html>
document.getElementById("heading") β Selects the <h1> element.
.innerText = "Hello, JavaScript!" β Changes its content.
<
<button onclick="changeColor()">Change Background</button>
<script>
function changeColor() {
document.body.style.backgroundColor = "lightblue";
}
</script>
document.body.style.backgroundColor = "lightblue"; β Directly modifies the CSS property.
<style>
.highlight { color: red; font-weight: bold; }
</style>
<p id="text">This is a paragraph.</p>
<button onclick="toggleHighlight()">Toggle Highlight</button>
<script>
function toggleHighlight() {
document.getElementById("text").classList.toggle("highlight");
}
</script>
Date: 2025-03-29 00:00:00.000000