Event based programs
1. Write a JavaScript program to change background color of document.
<html>
<head>
<script language="Javascript">
function col()
{
[Link]="Red";
}
</script>
</head>
<body>
<form name=f1>
<input type="button" name="b1" value="color" onClick ="col()">
</form>
</body>
</html>
2. Write a JavaScript to calculate the sum of first 10 natural nos using event driven program.
<html>
<head>
<script language="Javascript">
function nat()
{
var i,t;
t=0;
[Link]("First ten natural numbers are:");
for(i=1;i<=10;i++)
{
t=t+i;
[Link]("<br>"+i);
}
[Link]("<br>");
[Link]("Sum="+t);
}
</script>
</head>
<body>
<form name=f1>
<input type="button" name="b1" value="Natural nos" onClick ="nat()">
</form>
</body>
</html>
3. Write JavaScript event based program to display table of any given number.
<html>
<head>
<script language="Javascript">
function tab()
{
var a;
a=[Link];
[Link]("Table of ",a, ":");
for(i=1;i<=10;i++)
{
t=i*a;
[Link]("<br>"+t);
}
}
</script>
</head>
<body>
<form name=f1>
Enter nay no: <input name=t1><br>
<input type="button" name="b1" value="Table" onClick ="tab()">
</form>
</body>
Output
4. Write event driven JavaScript program to calculate the reverse of the given number.
<html>
<head>
<script language="Javascript">
function rev()
{
var n,r,s;
s=0;
n=[Link];
while(n!= 0)
{
r = n % 10;
s = s * 10 + r;
n = parseInt(n/10);
}
[Link]("The reverse is " + s);
}
</script>
</head>
<body>
<form name=f1>
<input type="text" name="t1"> <br>
<input type="button" name="a" value="Find" onClick ="rev">
</form>
</body>
</html>
5. Write a event driven JavaScript code to display all even numbers between any two given
numbers.
<html>
<head>
<script language="Javascript">
function even()
{
var a,b,i;
a=parseInt([Link]);
b=parseInt([Link]);
[Link]("Even nos betn ", a , " and ",b, " are : <br>");
for(i=a;i<=b;i++)
{
if(i % 2 == 0)
{
[Link]("<br>"+i);
}
}
}
</script>
</head>
<body>
<form name=f1>
Enter 1st no: <input name=t1><br>
Enter 2nd no: <input name=t2><br>
<input type="button" name="b1" value="Even nos." onClick ="even()">
</form>
</body>
</html>
Output
6. Write event driven JavaScript code to calculate double of the given number.
<html>
<head>
<script language="Javascript">
/*To calculate the double of any given no. */
function test()
{
[Link]("Double: ", [Link] * 2);
}
</script>
</head>
<body>
<input type="text" name="n1"> <br>
<input type="button" name="a" value="Calculate" onClick ="test()">
</body>
</html>
7. Write a JavaScript code using mouse event so that by moving the mouse pointer over the text,
the image should display.
<html>
<head>
<title> JavaScript Program </title>
<script language = "JavaScript">
function iload()
{
[Link]('<img src="[Link]">');
[Link]("Welcome");
}
</script>
</head>
<body>
<a onMouseover="iload();">Welcome</a>
</body>
</html>
8. Write a JavaScript program to find greatest no. from any three given nos.
<html>
<head>
<script language ="Javascript">
//JavaScript to find greatest no.
function test()
{
a=[Link];
b=[Link];
c=[Link];
[Link](“Greatest no. is ");
if(a>=b && a>=c)
{
[Link](a);
}
else
{
if(b>=a && b>=c)
{
[Link](b);
}
else
{
[Link](c);
}
}
}
</script>
</head>
<body>
Enter any three nos:
<br>
<input type="text" name="n1"> <br>
<input type="text" name="n2"> <br>
<input type="text" name="n3"> <br>
<input type="button" name="button1" value="Check" onClick = "test()">
</body>
</html>
9. Write a JavaScript to change background color of the document by using mouseover event of
button.
<html>
<head>
<title> JavaScript Program </title>
<SCRIPT LANGUAGE = "JavaScript">
//Use of Mouseover and Mouseout event.
function test(col)
{
[Link]=col;
}
</SCRIPT>
</head>
<body>
<input type="button" name="n1" value="Red" onMouseover="test('red')"
onMouseout="test('black')">
<input type="button" name="n2" value="Blue" onMouseover="test('blue')"
onMouseout="test('green')">
</body>
</html>
10. Write a JavaScript script to accept the first, middle and last names of the user and print the
name.
<html>
<head>
<script language ="Javascript">
//JavaScript to find greatest no.
function test()
{
a=[Link];
b=[Link];
c=[Link];
[Link]("Name is: ",a +" "+ b+" "+c);
}
</script>
</head>
<body>
F_Name: <input type="text" name="n1"> <br>
M_Name: <input type="text" name="n2"> <br>
L_Name: <input type="text" name="n3"> <br>
<input type="button" name="button1" value="Join" onClick = "test()">
</body>
</html>
Output
In this way all others……………..
………………………………………………
……………………………………………….
THANKS