Practical 4
Practical 4
Write JavaScript functions and control the different components of Web page by
predefined JavaScript objects.
JavaScript provides predefined objects and functions that allow us to control different components of
a web page dynamically. These predefined objects interact with the Document Object Model (DOM),
enabling us to manipulate HTML and CSS elements based on user interaction or certain conditions.
To modify text or HTML content on a web page, we can use methods provided by the document
object.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Text Example</title>
</head>
<body>
<h1 id="mainHeading">Welcome to My Web Page</h1>
<button onclick="changeText()">Click Me</button>
<script>
function changeText() {
document.getElementById("mainHeading").innerHTML = "Hello, JavaScript!";
}
</script>
</body>
</html>
In this example, clicking the button triggers the changeText() function, which changes the heading text
to "Hello, JavaScript!" using the innerHTML property.
JavaScript allows to control CSS styles using the style property on DOM elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Style Example</title>
</head>
<body>
<p id="paragraph">This is a sample paragraph.</p>
<button onclick="changeStyle()">Change Style</button>
<script>
function changeStyle() {
let paragraph = document.getElementById("paragraph");
paragraph.style.color = "blue";
paragraph.style.fontSize = "20px";
paragraph.style.fontWeight = "bold";
}
</script>
</body>
</html>
In this example, the changeStyle() function changes the text color, font size, and font weight of the
paragraph when the button is clicked.
JavaScript has event objects, like onclick, onmouseover, and onmouseout, which can trigger specific
functions based on user interaction.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hover Example</title>
</head>
<body>
<button onmouseover="changeColor()" onmouseout="resetColor()">Hover Over Me</button>
<script>
function changeColor() {
document.body.style.backgroundColor = "lightblue";
}
function resetColor() {
document.body.style.backgroundColor = "white";
}
</script>
</body>
</html>
Here, the background color of the page changes to light blue when hovering over the button and resets
when the mouse leaves the button.
JavaScript can interact with form elements, making it possible to validate inputs, change
values, and process data dynamically.
Example: Basic Form Validation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Validation Example</title>
</head>
<body>
<form onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name">
<input type="submit" value="Submit">
</form>
<p id="errorMsg" style="color: red;"></p>
<script>
function validateForm() {
let name = document.getElementById("name").value;
let errorMsg = document.getElementById("errorMsg");
In this example, the validateForm() function checks if the input is empty when the form is submitted.
If it is, an error message is displayed, and the form is not submitted.
JavaScript allows to dynamically set or modify attributes of HTML elements, such as src for
images or href for links.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Image Example</title>
</head>
<body>
<img id="image" src="image1.jpg" alt="Image" width="200">
<button onclick="changeImage()">Change Image</button>
<script>
function changeImage() {
let image = document.getElementById("image");
image.src = "image2.jpg"; // Replace with the new image source
}
</script>
</body>
</html>
In this example, the changeImage() function modifies the src attribute of an image when the button is
clicked, effectively changing the displayed image.
JavaScript allows to hide or display elements using the display CSS property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Toggle Visibility Example</title>
</head>
<body>
<p id="message">Hello, this is a toggled message!</p>
<button onclick="toggleMessage()">Toggle Message</button>
<script>
function toggleMessage() {
let message = document.getElementById("message");
Here, the toggleMessage() function shows or hides the paragraph each time the button is clicked.
We can add or remove classes from elements to change their styling dynamically.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Toggle Class Example</title>
<style>
.highlight {
background-color: yellow;
font-weight: bold;
}
</style>
</head>
<body>
<p id="text">This is a paragraph of text.</p>
<button onclick="toggleHighlight()">Highlight Text</button>
<script>
function toggleHighlight() {
let text = document.getElementById("text");
text.classList.toggle("highlight");
}
</script>
</body>
</html>
In this example, clicking the button will add or remove the highlight class from the paragraph, which
changes the text style based on the defined CSS.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Element Example</title>
</head>
<body>
<ul id="myList">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<button onclick="addItem()">Add Item</button>
<script>
function addItem() {
let list = document.getElementById("myList");
let newItem = document.createElement("li");
newItem.textContent = "New Item";
list.appendChild(newItem);
}
</script>
</body>
</html>
In this example, clicking the button triggers the addItem() function, which creates a new list item and
adds it to the unordered list.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Remove Element Example</title>
</head>
<body>
<p id="textToRemove">This is a removable paragraph.</p>
<button onclick="removeText()">Remove Text</button>
<script>
function removeText() {
let text = document.getElementById("textToRemove");
text.remove();
}
</script>
</body>
</html>
In this example, clicking the button removes the paragraph from the DOM.