CSS_PR
CSS_PR
<html>
<body>
<h2>Arithmetic Operations</h2>
<input id="num1" type="number" placeholder="Enter first number">
<input id="num2" type="number" placeholder="Enter second number">
<select id="operation">
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select>
<button onclick="calculate()">Calculate</button>
<p id="result"></p>
<script>
function calculate() {
let num1 = parseInt(document.getElementById("num1").value);
let num2 = parseInt(document.getElementById("num2").value);
let operation = document.getElementById("operation").value;
let result;
switch (operation) {
case 'add':
result = num1 + num2;
break;
case 'subtract':
result = num1 - num2;
break;
case 'multiply':
result = num1 * num2;
break;
case 'divide':
if (num2 !== 0) {
result = num1 / num2;
} else {
result = "Error: Division by zero is not allowed.";
}
break;
default:
result = "Error: Invalid operation.";
}
<script>
let array = [1, 2, 3, 4, 5];
function findNumber() {
let number = parseInt(document.getElementById("number").value);
if (array.includes(number)) {
alert("Number found in array");
} else {
alert("Number not found in array");
}
}
</script>
</body>
</html>
</script>
</body>
</html>
<script>
function checkPalindrome() {
let string = document.getElementById("string").value;
let reversedString = string.split("").reverse().join("");
<script>
function checkOddEven() {
let number = parseInt(document.getElementById("number").value);
if (number % 2 === 0) {
alert("Number is even");
} else {
alert("Number is odd");
}
}
</script>
</body>
</html>
Write JavaScript program to illustrate the use of getter method and setter method.
<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
var car = {
color: "Red",
brand: "Ford",
get company() {
return this.brand;
},
set company(value) {
this.brand = value;
}
};
<script>
function checkNumber() {
let number = parseInt(document.getElementById("number").value);
if (number > 0) {
alert("Number is positive");
} else if (number < 0) {
alert("Number is negative");
} else {
alert("Number is zero");
}
}
</script>
</body>
</html>
Write a HTML code to create types of buttons on form and Write JavaScript code to
implement onsubmit event and onReset
<html>
<body>
<form onsubmit="return submitForm()" onreset="resetForm()">
<input type="text" name="name" placeholder="Enter name"><br><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
<input type="button" value="Click Me" onclick="buttonClick()">
</form>
<script>
function submitForm() {
alert("Form submitted");
return false;
}
function resetForm() {
alert("Form reset");
}
function buttonClick() {
alert("Button clicked");
}
</script>
</body>
</html>
<script>
function findFactorial() {
let number = parseInt(document.getElementById("number").value);
let factorial = 1;
<script>
function checkPalindrome() {
let string = document.getElementById("string").value;
let reversedString = string.split("").reverse().join("");
<script>
function addNumbers() {
let num1 = parseInt(document.getElementById("num1").value);
let num2 = parseInt(document.getElementById("num2").value);
let result = num1 + num2;
alert("Result: " + result);
}
</script>
</body>
</html>
Write a HTML code to create types of buttons on form and Write JavaScript code to
implement onsubmit event and onReset event.
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Form with Button Types</h1>
<form id="exampleForm" onsubmit="handleSubmit()" onreset="handleReset()">
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your name"
><br><br>
<label for="email">Email:</label>
<input type="text" id="email" name="email" placeholder="Enter your email"
><br><br>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
<button type="button" onclick="alert('You clicked a button!')">Click
Me</button>
</form>
<script>
// Handle form submission
function handleSubmit()
{
alert("Form submitted");
}
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Form Events: onFocus and onBlur</h1>
<form id="exampleForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name" onfocus="handleFocus()"
onblur="handleBlur()"><br><br>
Ans:<p id="id1"></p>
</form>
<script>
// Handle onFocus event
function handleFocus(element) {
document.getElementById("id1").innerHTML="Focus Gain";
}
<html>
<body onmousemove="display7()">
<p id="id1">Ans1:</p>
<p id="id2">Ans2:</p>
<p id="id3">Ans3:</p>
<p id="id4">Ans4:</p>
<p id="id5">Ans5:</p>
<p id="id6">Ans6:</p>
<form name="MyForm" >
<input type="button" name="b1" value="Click" onclick="display1()"
onmouseover="display3()"onmouseout="display4()">
<input type="button" name="b2" value="DB Click"
ondblclick="display2()"onmousedown="display5()"onmouseup="display6()">
<input type="button" name="b3" value=" Click me" >
</form>
<script >
function display1()
{
document.getElementById("id1").innerHTML="You Clicked the
button";
}
function display2()
{
document.getElementById("id2").innerHTML="You have
double clicked the button..";
}
function display3()
{
document.getElementById("id3").innerHTML="Mouse Present
on element.";
document.getElementById("id4").innerHTML="Ans4";
}
function display4()
{
document.getElementById("id4").innerHTML="Mouse goes
out from element.";
document.getElementById("id3").innerHTML="Ans3";
}
function display5()
{
document.getElementById("id5").innerHTML="Mouse down";
document.getElementById("id6").innerHTML="Ans6";
}
function display6()
{
document.getElementById("id6").innerHTML="Mouse up";
document.getElementById("id5").innerHTML="Ans5";
}
function display7()
{
console.log("mouse moved");
}
</script>
</body>
</html>
<html>
<body>
<input type="text" id="t1" placeholder="Enter Key"><br><br>
<input type="text" id="t2" placeholder="Enter value"><br><br>
<input type="button" id="b1" value="Create Cookie"
onclick="create_cookie()"><br><br>
<input type="button" id="b2" value="Read cookie"
onclick="read_cookie()"><br><br>
<input type="button" id="b4" value="Delete Cookie"
onclick="delete_cookie()"><br><br>
<hr>
Your Cookies:<p id="p1"></p>
</hr>
<script>
function create_cookie()
{
var key=document.getElementById("t1").value;
var value=document.getElementById("t2").value;
document.cookie=key+"="+value+";expires= Sun, 10 Sept 2025
00:00:00 UTC ;path=/;";
alert("Cookie Created");
}
function read_cookie()
{
var x=document.cookie;
document.getElementById("p1").innerHTML=x;
}
function delete_cookie()
{
var key=document.getElementById("t1").value;
var value=document.getElementById("t2").value;
document.cookie=key+"="+value+";expires= Sun, 10 Sept 1970
00:00:00 UTC ; path=/;"
alert("Cookie Created");
}
</script>
</body>
</html>
Write JavaScript program to check whether the string contains the given pattern or not.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Regex Match</title>
</head>
<body>
<h1>Regex Match Demo</h1>
<p>Enter a pattern to search in the text:</p>
<input type="text" id="patternInput" >
<button onclick="matchPattern()">Match</button>
<p id="result">Result: </p>
<script type="text/javascript">
function matchPattern() {
// Get the input pattern
var userPattern = document.getElementById("patternInput").value;
// Text to search
var text = "I Love apples and apples are delicious.";
<html>
<body>
<h1>Text Roll Over Example</h1>
<br><br>
<a onmouseover="display1()">XYZ
|<a onmouseover="display2()">ABC<br><br>
<p id="p1">Hello World</p>
<a> <img src="xyz.jpg" alt="ABC image" name="form1" height="500"
width="500"></a>
<script>
function display1()
{
document.form1.src="xyz.jpg";
document.getElementById("p1").innerHTML="You have selected
XYZ";
}
function display2()
{
document.form1.src="ABC.jpg";
document.getElementById("p1").innerHTML="You have selected
ABC";
}
</script>
</body>
</html>
Write JavaScript program to change the images in banner.
<html>
<head>
<title>Array of Banners</title>
</head>
<body>
<img src="1.jpg" width="50%" id="image1" />
<br /><br />
<button onclick="display()">Display Banners</button>
<script type="text/javascript">
var banner_array = new Array("1.jpg", "2.jpg", "3.jpg",
"4.jpg", "5.jpg");
var timerID = null;
var i = 0;
function display() {
if (timerID == null) {
timerID = setInterval("display_banner()", 1000);
}
}
function display_banner() {
if (i < banner_array.length) {
document.getElementById("image1").src = banner_array[i];
i = i + 1;
} else {
clearInterval(timerID);
}
}
</script>
</body>
</html>
Write JavaScript program to create a slide show.
<!DOCTYPE html>
<html>
<head>
<title>Image SlideShow</title>
<script type="text/javascript">
var images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"];
var count = 0;
function previousImage() {
if (count > 0) {
count--;
}
var id = document.getElementById("imageId");
id.src = "" + images[count];
}
function nextImage() {
if (count < images.length - 1) {
count++;
}
var id = document.getElementById("imageId");
id.src = "images/" + images[count];
}
</script>
</head>
<body>
<center>
<script>
function calculateGrade() {
// Get the input value
const marksInput = document.getElementById("marks").value;
<!DOCTYPE html>
<html >
<head>
<script type="text/javascript">
// Arrays for Fruits and Vegetables
var fruits_array = ["Apple", "Banana", "Cherry", "Mango"];
var vegetables_array = ["Carrot", "Broccoli", "Spinach", "Potato"];
Write a JavaScript that displays textboxes for accepting name and email id and a submit
button. Write JavaScript code such that when the user clicks on submit button a) Name
validation b) email ID validation.
<html >
<body>
<h1>Form Validation</h1>
<label for="name">Name:</label>
<input type="text" id="name" placeholder="Enter your name"><br><br>
<button onclick="validateForm()">Submit</button>
<script>
function validateForm() {
// Get the input values
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
// Name validation: Must not be empty and should only contain letters and spaces
if (!name) {
nameError.textContent = "Name is required.";
isValid = false;
} else if (!/^[a-zA-Z\s]+$/.test(name)) {
nameError.textContent = "Name should contain only letters and spaces.";
isValid = false;
}
// If the form is valid, show a success message or proceed with form submission
if (isValid) {
alert("Form submitted successfully!");
}
}
</script>
</body>
</html>
Write JavaScript for following frame structure
FRAME1
Frame 2 Frame 3
-Fruits
- Flowers
- Cities
Fruits, flowers and cities are links to the web page fruits.html, flowers.html, cities.html
respectively. When these links are clicked corresponding data appears in Frame 3.
Main Frame
<html>
<head>
<title>Three Frames Layout</title>
</head>
<frameset rows="20%,*">
<!-- Frame 1: Top header -->
<frame src="header.html" name="frame1" scrolling="no" noresize>
<frameset cols="30%,*">
<!-- Frame 2: Left links section -->
<frame src="links.html" name="frame2" scrolling="auto">
links.html
<!DOCTYPE html>
<html>
<head>
<title>Links</title>
</head>
<body>
<h3>Frame 2</h3>
<ul>
<li><a href="fruits.html" target="frame3">Fruits</a></li>
<li><a href="flowers.html" target="frame3">Flowers</a></li>
<li><a href="cities.html" target="frame3">Cities</a></li>
</ul>
</body>
</html>
Write JavaScript program to create a pull down menu with three options
(Google,MSBTE,Yahoo)
Once the user will select one of the options then the user will be redirected to that site.
<!DOCTYPE html>
<html>
<body>
<h1>Pull Down Menu</h1>
<select id="menu" onchange="redirectToSite()">
<option value="">-- Select an option --</option>
<option value="https://www.google.com">Google</option>
<option value="https://msbte.org.in">MSBTE</option>
<option value="https://www.yahoo.com">Yahoo</option>
</select>
<script>
function redirectToSite() {
const menu = document.getElementById("menu");
const selectedValue = menu.value;
if (selectedValue) {
window.location.href = selectedValue; // Redirects to the selected site
}
}
</script>
</body>
</html>