Switch ,Array, Events in JavaScript
Switch
<html>
<head>
<title>Switch Case with Buttons</title>
</head>
<body>
<label for="a">Enter the first number:</label>
<input type="number" id="num1"><br>
<label for="b">Enter the second number:</label>
<input type="number" id="num2"><br><br>
<button id="button1">Add</button>
<button id="button2">Mod</button>
<button id="button3">Del</button><br><br>
<div id="show"></div>
<script>
function fun1()
{
const a = parseFloat(document.getElementById("num1").value);
const b = parseFloat(document.getElementById("num2").value);
const c=a+b;
document.getElementById('show').innerHTML = 'Addition is ='+c ;
}
function fun2()
{
const a = parseFloat(document.getElementById("num1").value);
const b = parseFloat(document.getElementById("num2").value);
const c=a*b;
document.getElementById('show').innerHTML = 'Modification is = '+c ;
}
function fun3()
{
const a = parseFloat(document.getElementById("num1").value);
const b = parseFloat(document.getElementById("num2").value);
const c=a-b;
document.getElementById('show').innerHTML = 'Deletion is = '+c ;
}
document.querySelectorAll('button').forEach(button => {
button.addEventListener('click', function() {
switch (this.id) {
case 'button1':
fun1();
break;
case 'button2':
fun2();
break;
case 'button3':
fun3();
break;
}
});
});
</script>
</body>
</html>
OUTPUT
Array
<html>
<body>
<h1> Array</h1>
<p>Display an array elements individually:</p>
<p id="ar1"></p>
<p>Display an array elements with toString() method:</p>
<p id="ar2"></p>
<script>
const color = ["Red", "Orange", "green", "blue"];
document.getElementById("ar1").innerHTML = color[0]+','+color[1];
const fruits = ["Red", "Orange", "green", "blue"];
document.getElementById("ar2").innerHTML = fruits.toString();
</script>
</body>
</html>
JavaScript Events
OnLoad Event
<html>
<head><title>onload event</title></head>
<body onload="show()">
<h1>The onload Event</h1>
<h2><p id="show"></h2>
<script>
function show() {
document.getElementById('show').innerHTML = 'wel come';
}
</script>
</body>
</html>
OUTPUT
Onchange Event
<html>
<head>
<title>Onchange Event</title>
</head>
<body>
<label for="num1">Enter first number:</label>
<input type="number" id="num1" name="num1"><br><br>
<label for="num2">Enter second number:</label>
<input type="number" id="num2" name="num2"><br><br>
<label for="operation">Select operation:</label>
<select id="operation" name="operation" onchange="calculate()">
<option value="add">Addition</option>
<option value="mod">Modification</option>
<option value="del">Deletion</option>
</select><br><br>
<p id="result"></p>
<script>
function calculate() {
let num1 = parseFloat(document.getElementById("num1").value);
let num2 = parseFloat(document.getElementById("num2").value);
let operation = document.getElementById("operation").value;
let result;
function add()
{
result = num1 + num2;
}
function mod()
{
result = num1 * num2;
}
function del()
{
result = num1 - num2;
}
switch (operation) {
case "add":
add();
break;
case "mod":
mod();
break;
case "del":
del();
break;
default:
result = "Invalid operation";
}
document.getElementById("result").textContent = "Result: "+result ;
}
calculate();
</script>
</body>
</html>
OUTPUT
OnMouseover ,OnMouseout Event
<html>
<head>
<title>onmouseover onmouseout Event</title>
<style>
#myDiv {
background:url("p11.jpg");
width:250px;
height:250px;
background-size:cover;
background-position:center;
display: none; /* Initially hidden */
}
button{
height:60px;
width:80px;
}
</style>
</head>
<body>
<p>Move the mouse over the button to display the image.</p>
<div id="myDiv">
This is the hidden div.
</div>
<button id="button1"onmouseover="showDiv()" onmouseout="hideDiv()">Display</button>
<script>
function showDiv() {
document.getElementById("myDiv").style.display = "block";
button1.style.display = "none"
}
function hideDiv() {
document.getElementById("myDiv").style.display = "none";
button1.style.display = "block"
}
</script>
</body>
</html>
OUTPUT
OnMouseover ,OnMouseout Event
<html>
<head>
<title>Onchange Event</title>
<style>
#myDiv{
background:url("p11.jpg");
width:250px;
height:250px;
background-size:cover;
background-position:center;
display ="block";
}
</style>
</head>
<body>
<div id="myDiv">
</div>
<script>
const myDiv = document.getElementById("myDiv");
myDiv.onmouseover = function() {
document.getElementById("myDiv").style.width = "500px";
document.getElementById("myDiv").style.height = "500px";
};
myDiv.onmouseout = function() {
document.getElementById("myDiv").style.width = "250px";
document.getElementById("myDiv").style.height = "250px";
};
</script>
</body>
</html>
OUTPUT
OnMouseout
Comments
Post a Comment