0% found this document useful (0 votes)
17 views10 pages

JavaScript Programming Assignments Guide

The document contains a series of assignments demonstrating various JavaScript concepts, including embedding JavaScript in HTML, conditional statements, looping statements, string functions, and event handling. Each assignment provides code examples for different functionalities such as form validation, dialog boxes, and date objects. The assignments are structured to help learners understand and apply JavaScript in web development.

Uploaded by

abhimain1137
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views10 pages

JavaScript Programming Assignments Guide

The document contains a series of assignments demonstrating various JavaScript concepts, including embedding JavaScript in HTML, conditional statements, looping statements, string functions, and event handling. Each assignment provides code examples for different functionalities such as form validation, dialog boxes, and date objects. The assignments are structured to help learners understand and apply JavaScript in web development.

Uploaded by

abhimain1137
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment :: 1

1).Write a program to embed JavaScript into HTML.


<html>
<head>
<title>Javascript:Good Morning All Of U</title>
<script type="javascript">
alert("Good Morning All Of U");
</script>
</head>
<body>
<p>This Message Only Display A Message Box.</p>
</body>
</html>
Assignment :: 2
2).Write a JavaScript code to demonstrate Conditional Statements.
<html>

<body>

<script language="javascript">

var result,n;
result=parseInt(prompt("Enter A Number"));

if(result >=35)

[Link]("Pass");

else

[Link]("Fail");

</script>

</body>

</html>
Assignment :: 3
3).Write a JavaScript code to Demonstrate Looping Statements.
 For Loop
<html>
<body>
<script language="javascript">
var i;
for(i=1;i<=10;i++)
{
[Link](i+"<br>");
}
</script>
</body>
</html>

 While Loop
<html>
<body>
<script language="javascript">
var i;
i=1;
while(i<=10)
{
[Link](i+"<br>");
i=i+1;
}
</script>
</body>
</html>
Assignment :: 4
4).Write JavaScript code to demonstrate Different String Function.
<html>
<body>
<script language="javascript">
var x="INDIA";
[Link]([Link](2));
[Link]("\n"+[Link](3));
var a="Hello";
var b="Friends";
[Link]("\n"+[Link](b));
var c="green";
[Link]("\n"+[Link]("een","ay"));
[Link]("\n"+[Link]());
[Link]("\n"+[Link]());
[Link]("\n"+[Link]('D'));
[Link]("\n"+[Link]("I"));
[Link]("\n"+[Link](0,3));
</script>
</body>
</html>
Assignment :: 5
5).Write JavaScript code to demonstrate onblur,onfocus,onload,onsubmit.
 Onblur & Onfocus Event
<html> </script>
<body> </body>
<h1>HTML DOM Events</h1> </html>
<h2>The onfocus and blur  Onsubmit Event
Events</h2>
<html>
<p>When you enter the input field,a
function sets <body>

the background color to yellow.</p> <p>Assign an "Onsubmit" event to a


form element</p>
<p>When you leave the input field,a
function sets the <p>when you submit the form, a
function is triggered which
background color to red</p>
alerts some text.</p>
Enter your name::<input type="text"
id="myInput" <form action="/action_page.php"
onsubmit="myFunction()">
onfocus="focusFunction()"
onblur="blurFunction()"> Enter name::<input
type="text" name="fname">
<script language="javascript">
<input type="submit"
function focusFunction() value="submit">
{ </form>
<script language="javascript">
[Link]("myInput"
).[Link]="yellow"; function myFunction()

} {

function blurFunction() alert("The Form Was


Submitted");
{
}

[Link]("myInput" </script>
).[Link]="red";
</body>
}
</html>
Assignment :: 6
6).Write JavaScript code to demonstrate onkeypress, onmouseove
,onmouseout.
//onkeypress

<html> {

<body> alert("This is JavaTpoint");

<h1>HTML DOM Events</h1> }

<h2>The onkeypress Event</h2> //-->

<p>A function is triggered when the user is </script>


pressing a key in the input field.</p>
<p onmouseover="mouseoverevent()"> Keep
<input type="text" cursor over me</p>
onkeypress="myFunction()">
</body>
<script>
</html>
function myFunction() {
// onmouseout
alert("You pressed a key inside the input
<html>
field");
<head>
}
<script>
</script>
<!--
</body>
function sayHello() {
</html>
alert("Mouse Out")
// onmouseover
}
<html>
//-->
<head>
</script>
<h1> Javascript Events </h1>
</head>
</head>
<body>
<body>
<p onmouseout = "sayHello()">This is demo
<script language="Javascript"
text for mouseover event.</p>
type="text/Javascript">
</body>
<!--
</html>
function mouseoverevent()
Assignment :: 7
7).Write a programto perform addition of two numbers using web form.
<html>
<head>
<script>
function add()
{
var numOne, numTwo, sum;
numOne = parseInt([Link]("first").value);
numTwo = parseInt([Link]("second").value);
sum = numOne + numTwo;
[Link]("answer").value = sum;
}
</script>
</head>
<body>
<p>Enter the First Number: <input id="first"></p>
<p>Enter the Second Number: <input id="second"></p>
<button onclick="add()">Add</button>
<p>Sum = <input id="answer"></p>
</body>
</html>
Assignment :: 8
8).Create a HTML page to demonstrate Date object using web form.
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>Create a new date object with the current date and time:</p>
<p id="demo"></p>
<script>
const d = new Date();
[Link]("demo").innerHTML = d;
</script>
</body>
</html>
Assignment :: 9
9).Write a Javascript code to demonstrate use of Dialog Box.
//Alert Dialog Box [Link] ("User does not want to
continue");
<html>
}
<head>
}
<script type="text/javascript">
</script> </head>
function show() {
<body> <center>
alert("It is an Alert dialog box");
<h1>Hello World :) :)</h1>
}
<h2>Welcome to javaTpoint</h2>
</script>
<p>Click the following button </p>
</head>
<input type="button" value="Click Me"
<body> onclick="show();" />
<center> </center> </body> </html>
<h1>Hello World :) :)</h1> // Prompt Dialog Box
<h2>Welcome to javaTpoint</h2> <html> <head>
<p>Click the following button </p> <script type="text/javascript">
<input type="button" value="Click Me" function show() {
onclick="show();" />
var value = prompt("Enter your Name : ",
</center> "Enter your name");
</body> [Link]("Your Name is : " + value);
</html> }
// Confirm Dialog Box </script> </head>
<html> <body> <center>
<head> <h1>Hello World :) :)</h1>
<script type="text/javascript"> <h2>Welcome to javaTpoint</h2>
function show() { <p>Click the following button </p>
var con = confirm ("It is a Confirm dialog <input type="button" value="Click Me"
box"); onclick="show();" />
if(con == true) { </center>
[Link] ("User Want to continue"); </body>
}
</html>
else {
Assignment :: 10
10).Write a Javascript to apply form validation-not null,number,string.
//Number Validation <p>Try to submit the form without entering any
text.</p>
<script>
<form name="myForm" action="/action_page.php"
function validate(){
onsubmit="return
var num=[Link];
validateForm()" method="post" required>
if (isNaN(num)) {
Name: <input type="text" name="fname">
[Link]("numloc").innerHTML="E
<input type="submit" value="Submit">
nter Numeric value only";
</form> </body> </html>
return false;
// String Validation
} else {
<script>
return true;
function validateform(){
}
var name=[Link];
}
var password=[Link];
</script>
if (name==null || name==""){
<form name="myform" onsubmit="return
validate()" > alert("Name can't be blank");

Number: <input type="text" name="num"><span return false;


id="numloc"></span><br/>
}else if([Link]<6){
<input type="submit" value="submit">
alert("Password must be at least 6 characters
</form> long.");

// null Validation return false;

<html> <head> <script> }

function validateForm() { }

var x = [Link]["myForm"]["fname"].value; </script> <body>

if (x == "" || x == null) { <form name="myform" method="post"


action="[Link]" onsubmit="return valid
alert("Name must be filled out");
ateform()" >
return false;
Name: <input type="text" name="name"><br/>
}
Password: <input type="password"
}
name="password"><br/>
</script> </head><body>
<input type="submit" value="register">
<h2>JavaScript validation for empty input field</h2>
</form>

You might also like