JS lab
JS lab
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Insertion Methods</title>
<style>
body {
font-family: Arial, sans-serif;
}
#output {
margin-top: 20px;
font-weight: bold;
}
</style>
<script>
// Internal JavaScript
function internalFunction() {
document.getElementById('output').innerText = 'This is from the internal JavaScript!';
}
</script>
</head>
<body>
<div id="output"></div>
<h1>NoScript Example</h1>
<div id="message"></div>
<noscript>
<div style="color: red; font-weight: bold;">
JavaScript is disabled in your browser. Please enable it for the best experience.
</div>
</noscript>
</body>
</html>
Lab 3 write a program to read an element DIV in HTML document using querySelector() method and
change its text content.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change DIV Text Content</title>
<style>
body {
font-family: Arial, sans-serif;
}
#myDiv {
padding: 20px;
border: 1px solid #ccc;
margin: 20px 0;
background-color: #f9f9f9;
}
</style>
<script>
// Function to change the text content of the DIV
function changeDivText() {
// Use querySelector to select the DIV with the ID 'myDiv'
const myDiv = document.querySelector('#myDiv');
// Change the text content of the selected DIV
myDiv.textContent = 'The text content has been changed!';
}
</script>
</head>
<body>
</body>
</html>
Lab4 Write a function to add two numbers and display the result in a text box.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Two Numbers</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
input {
margin: 5px;
padding: 10px;
width: 100px;
}
button {
padding: 10px 15px;
margin: 5px;
}
#result {
margin-top: 20px;
font-weight: bold;
}
</style>
<script>
// Function to add two numbers and display the result
function addNumbers() {
// Get the values from the input fields
const num1 = parseFloat(document.querySelector('#num1').value);
const num2 = parseFloat(document.querySelector('#num2').value);
<button onclick="addNumbers()">Add</button>
</body>
</html>
Lab 5 write a function to validate a form with the fields student name email and address.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation Example</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
input, textarea {
margin: 5px;
padding: 10px;
width: 300px;
display: block;
}
button {
padding: 10px 15px;
margin: 5px;
}
.error {
color: red;
font-weight: bold;
}
</style>
<script>
// Function to validate the form
function validateForm() {
// Get the values from the input fields
const name = document.querySelector('#studentName').value.trim();
const email = document.querySelector('#email').value.trim();
const address = document.querySelector('#address').value.trim();
const errorMessage = document.querySelector('#errorMessage');
<label for="email">Email:</label>
<input type="email" id="email" placeholder="Enter email" required />
<label for="address">Address:</label>
<textarea id="address" placeholder="Enter address" required></textarea>
<button type="submit">Submit</button>
</form>
</body>
</html>
Lab6 Write a function that creates a html element and invoke that function upon clicking on a button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create HTML Element on Button Click</title>
</head>
<body>
<script>
// Function to create a new HTML element
function createElement() {
// Create a new paragraph element
const newElement = document.createElement('p');
// Set the content of the paragraph
newElement.textContent = 'This is a newly created paragraph element!';
// Append the new element to the container
document.getElementById('elementContainer').appendChild(newElement);
}
</body>
</html>
Lab 7.Prepare an array as a collection of mixed types of data i.e string number Boolean .
Lab 8 Write a program to show a loop using while for and for each.
Lab 9. Write a program to log array element values in console.
Lab 10. Prepare an object of employees with properties.
Lab 11