java script lab manual edited (1)
java script lab manual edited (1)
Learn
!DOCTYPE html>
<html>
<head>
<title>JavaScript Basics</title>
</head>
<body>
<h1>Hello, JavaScript!</h1>
<script>
// Single-line comment
console.log("Hello, Console!");
document.write("Hello, World!");
alert("Hello, Alert!");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Inline Events</title>
</head>
<body>
<input type="text" onchange="alert('Value changed to: ' +
this.value);" placeholder="Type something...">
</body>
</html>
C. Mouse over Event
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Inline Events</title>
</head>
<body>
</p>
</body>
</html>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>HTML Manipulation Example</title>
<style>
#main-heading {
color: blue;
}
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<h1 id="main-heading">Hello, World!</h1>
<p class="description">This is a paragraph.</p>
<p class="description">Another paragraph here.</p>
<button id="change-text">Change Heading Text</button>
<button id="highlight-paragraphs">Highlight Paragraphs</button>
<button id="add-paragraph">Add New Paragraph</button>
<div id="container"></div>
<script src="script.js"></script>
</body>
</html>
Experiment three:
Form validation Create a basic HTML form and link the java script
code scrept.js.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Interactive Form</title>
</head>
<body>
<h1>Registration Form</h1>
<label for="name">Name:</label>
<label for="email">Email:</label>
<label for="age">Age:</label>
<button type="submit">Submit</button>
</form>
<p id="output"></p>
<script src="script.js"></script>
</body>
</html>
B. Create the java script code and save it as scrept.js it performs
the form validation
document.getElementById('registrationForm').addEventListener('submit',
function(event) {
event.preventDefault(); // Prevent form submission
<script>
// JavaScript Code
const contentArray = [
"Welcome to the multy data array page",
{ type: "image", src: "doge.jpg", alt: "Placeholder Image" },
{ type: "link", href: "file.html", text: "go to file page" }
];
// Reference to the content div
const contentDiv = document.getElementById("content");
// Render content dynamically
contentArray.forEach(item => {
if (typeof item === "string") {
// Render text
const paragraph = document.createElement("p");
paragraph.textContent = item;
contentDiv.appendChild(paragraph);
}
else if (item.type === "image") {
// Render image
const image = document.createElement("img");
image.src = item.src;
image.alt = item.alt;
contentDiv.appendChild(image);
}
else if (item.type === "link") {
// display the link
const link = document.createElement("a");
link.href = item.href;
link.textContent = item.text;
link.target = "_blank"; // Open in a new tab
contentDiv.appendChild(link);
}
});
</script>
</body>
</html>
Implementing lopping statements switch case
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var day = prompt("Enter a number between 1 and 7");
switch (day)
{
case (day="1"):
document.write("Sunday");
break;
case (day="2"):
document.write("Monday");
break;
case (day="3"):
document.write("Tuesday");
break;
case (day="4"):
document.write("wodnsday");
break;
case (day="5"):
document.write("Thursday");
break;
case (day="6"):
document.write("friday");
break;
case (day="3"):
document.write("saterday");
break;
default:
document.write("Invalid week day");
break;
}
</script>
</body>
</html>
While,do while ,and for loops
<!DOCTYPE html>
<html>
<head>
<title>Loops</title>
</head>
<body>
<script>
// For loop
for (let i = 1; i <= 5; i++) {
document.write(" for loop Iteration: " + i);
// While loop
let j = 1;
while (j <= 5) {
document.write("While Iteration: " + j);
j++;
}
// Do-while loop
let k = 1;
do {
document.write("Do-While Iteration: " + k);
k++;
} while (k <= 5);
</script>
</body>
</html>
Implementing conditional statements if else condition
<!DOCTYPE html>
<html><body>
<script type="text/javascript">var num = prompt("Enter Number");
if (num > 0)
{
alert("Given number is Positive!!!");
}
else if (num==0)
{
alert("it is Zero");
}
else
{
alert("it is negative nimber");
}
</script>
</body>
</html>