Exam JS
Exam JS
Write a HTML script which displays 2 radio buttons to the users for fruits
and vegetables and 1 option list. When user select fruits radio button
option list should present only fruits names to the user & when user select
vegetable radio button option list should present only vegetable names to
the user. (DYNAMICALLY)
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Option List</title>
<script>
function updateOptions(category) {
// Reference to the select element
var optionList = document.getElementById("options");
Q. Describe, how to read cookie value and write a cookie value. Explain
with example.
Web Browsers and Servers use HTTP protocol to communicate and HTTP is a
stateless protocol. But for a commercial website, it is required to maintain
session information among different pages. For example, one user registration
ends after completing many pages.
Storing Cookies
The simplest way to create a cookie is to assign a string value to the
document.cookie object, which looks like this. document.cookie = "key1 =
value1;key2 = value2;expires = date"; Here the expires attribute is optional. If you
provide this attribute with a valid date or time, then the cookie will expire on a
given date or time and thereafter, the cookies' value will not be accessible.
<html>
<head>
<script type="text/javascript">
function WriteCookie() {
var customerName = document.myform.customer.value;
Reading Cookies
Reading a cookie is just as simple as writing one, because the value of the
document.cookie object is the cookie. So you can use this string whenever you
want to access the cookie. The document.cookie string will keep a list of
name=value pairs separated by semicolons, where name is the name of a cookie
and value is its string value.
<html>
<head>
<script type="text/javascript">
function ReadCookie() {
var name = "name="; // Cookie key we're looking for
var decodedCookie = document.cookie; // Get all cookies
var ca = decodedCookie.split(';'); // Split cookies by semicolon
Q.Write a java script that displays textboxes for accepting name & email ID
& a submit button. Write java script code such that when the user clicks on
submit button (1) Name Validation (2) Email ID Validation
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
function validateForm() {
// Name validation
var name = document.getElementById("name").value;
if (name == "") {
alert("Name must be filled out");
return false;
}
// Email ID validation
var email = document.getElementById("email").value;
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (email == "") {
alert("Email must be filled out");
return false;
} else if (!emailPattern.test(email)) {
alert("Please enter a valid email ID");
return false;
}
<!DOCTYPE html>
<html>
<head>
<title>Checkbox Selection</title>
<script type="text/javascript">
function evaluateCheckbox() {
var checkbox = document.getElementById("subscribe");
if (checkbox.checked) {
alert("You are subscribed to the newsletter.");
} else {
alert("You are not subscribed.");
}
}
</script>
</head>
<body>
<h1>Subscribe to Newsletter</h1>
<form>
<label for="subscribe">Subscribe to Newsletter:</label>
<input type="checkbox" id="subscribe" name="subscribe">
<br><br>
<input type="button" value="Submit" onclick="evaluateCheckbox()">
</form>
</body>
</html>
Q. Write a javascript to create a pull-down menu with three options
[Google, MSBTE, Yahoo] once the user will select one of the options then
user will be redirected to that site.
<!DOCTYPE html>
<html>
<head>
<title>Redirect Based on Selection</title>
<script type="text/javascript">
function redirectToSite() {
var selectedSite = document.getElementById("siteSelect").value;
if (selectedSite != "") {
window.location.href = selectedSite; // Redirect to selected site
}
}
</script>
</head>
<body>
<h1>Select a Website</h1>
<form>
<label for="siteSelect">Choose a website:</label>
<select id="siteSelect" onchange="redirectToSite()">
<option value="">--Select--</option>
<option value="https://www.google.com">Google</option>
<option value="https://www.msbte.org.in">MSBTE</option>
<option value="https://www.yahoo.com">Yahoo</option>
</select>
</form>
</body>
</html>
Q. Explain text rollover with suitable example?
<!DOCTYPE html>
<html>
<head>
<title>Text Rollover Using onmouseover</title>
<script type="text/javascript">
// Function to change the text style on mouseover
function rolloverOn() {
var element = document.getElementById("text");
element.style.color = "#FF6347"; // Change text color
element.style.fontSize = "28px"; // Increase font size
}
</body>
</html>
Create a slideshow with the group of three images, also simulate next and
previous transition between slides in your Java Script.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Slideshow</title>
<script>
// Array of image filenames
var pics = new Array('1.jpg', '2.jpg', '3.jpg');
var count = 0;
</body>
</html>
Write a Java script program which computes, the average marks of the
following students then, this average is used to determine the
corresponding grade.
Student Name Marks Sumit 80 Kalpesh 77 Amit 88 Tejas 93 Abhishek 65.
The grades are computed as follows : Range Grade <60 E <70 D <80 C <90
B <100 A
<html>
<head>
<title>Compute the Average Marks and Grade</title>
</head>
<body>
<script>
// Student names and marks
var students = [
['Summit', 80],
['Kalpesh', 77],
['Amit', 88],
['Tejas', 93],
['Abhishek', 65]
];
var totalMarks = 0;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Non-Matching Characters Example</title>
<script>
function findNonMatchingChars() {
var str = "Hello World";
var regex = /[^aeiou]/g; // Match characters that are not vowels
var result = str.match(regex); // Get non-vowel characters
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Context Menu Example</title>
<script>
// Function to handle the right-click context menu
function showContextMenu(event) {
event.preventDefault(); // Prevent the default context menu from showing
// Get the context menu and position it based on the mouse position
var contextMenu = document.getElementById('contextMenu');
contextMenu.style.left = event.pageX + 'px';
contextMenu.style.top = event.pageY + 'px';
contextMenu.style.display = 'block'; // Display the custom context menu
}
// Add event listener for the document to hide the context menu on click
document.addEventListener('click', hideContextMenu);
</script>
</head>
<body>
<script>
// Add event listener to show the custom context menu when right-clicking on
the page
document.addEventListener('contextmenu', showContextMenu);
</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>Multiple Iframes Example</title>
</head>
<body>
<h1>Webpage with Three Iframes</h1>
<h2>Iframe 2: Wikipedia</h2>
<iframe src="https://www.wikipedia.org" width="600" height="400"
title="Wikipedia"></iframe>
</body>
</html>
Q.SLIDESHOW with links
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Slideshow with Links</title>
<script>
// Array of image URLs from Google
var pics = new Array(
'1.jpg', // Example image from Google
'2.jpg', // Example image from Google
'3.jpg' // Example image from Google
);
var count = 0;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Folding Tree Menu with "->"</title>
<style>
/* Basic styles for the menu */
ul {
list-style-type: none;
padding-left: 20px;
}
li {
cursor: pointer;
padding: 5px;
font-size: 16px;
}
.nested {
display: none;
}
.caret {
padding-right: 10px;
}
/* Symbol styles */
.folder-symbol {
font-weight: bold;
padding-right: 5px;
}
</style>
</head>
<body>
<script>
// Function to toggle the nested menu visibility
function toggleMenu(event) {
var currentCaret = event.target;
var nestedList = currentCaret.parentElement.querySelector('.nested');
</body>
</html>
Q. Write HTML code to design a form that display two textboxes for
accepting two numbers,one textbox for accepting result and two buttons as
ADDITION and SUBTRACTION.Write proper JavaScript such that when the
user clicks on any one of the button,respective operation will be performed
on two numbers and result will be displayed in result textbox.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator Form</title>
<script>
// Function to perform addition
function performAddition() {
var num1 = parseFloat(document.getElementById('number1').value);
var num2 = parseFloat(document.getElementById('number2').value);
// Perform addition
var result = num1 + num2;
document.getElementById('result').value = result;
}
// Perform subtraction
var result = num1 - num2;
document.getElementById('result').value = result;
}
</script>
</head>
<body>
<h2>Simple Calculator</h2>
<form>
<label for="number1">Enter First Number:</label>
<input type="text" id="number1" name="number1"><br><br>
<label for="result">Result:</label>
<input type="text" id="result" name="result" readonly><br><br>
Q. Write HTML code to design a form that dsiplays two buttons START and
STOP.Write a JavaScript code such that when user clicks on START button,
real time digital clock will be displayed on screen. When user clicks on
STOP button,clock will stop displaying time(Use Timer Methods)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real-Time Digital Clock</title>
<script>
var timer; // Variable to store the timer reference
Write a JavaScript that accents user's first name and domain name of
Organization from user., The JavaScript then forms email address as name
and displays the results in the browser window.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Email Address</title>
<script>
function createEmail() {
// Get values from the input fields
var firstName = document.getElementById("firstName").value.trim();
var domain = document.getElementById("domain").value.trim();
<label for="domain">Domain:</label>
<input type="text" id="domain" placeholder="Enter organization domain
(e.g., example.com)"><br><br>
Q.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laptop Brands</title>
<script>
function showLaptopImage(brand) {
// Map laptop brands to images
var laptopImages = {
"Dell": "https://via.placeholder.com/300x200?text=Dell",
"HP": "https://via.placeholder.com/300x200?text=HP",
"Lenovo": "https://via.placeholder.com/300x200?text=Lenovo",
"Apple": "https://via.placeholder.com/300x200?text=Apple",
"Asus": "https://via.placeholder.com/300x200?text=Asus"
};
<h2>Selected Laptop:</h2>
<img id="laptopImage"
src="https://via.placeholder.com/300x200?text=Select+a+Laptop" alt="Laptop
Image">
</body>
</html>
Q.Write a JavaScript that demonstrates use of floating menu alongwith
respective HTML script.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Floating Menu</title>
<style>
#menu {
position: fixed;
top: 40px;
left: 40px;
background: lightgray;
padding: 10px;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="menu">
<h4>Menu</h4>
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
</ul>
</div>