Unit-3 Form and Event Handling
Unit-3 Form and Event Handling
<form >
// form Elements
</form>
PROPERTIES AND METHODS OF FORM
� action : (specify an url)Backend script ready to process
your passed data.
� method : Method to be used to upload data. The most
frequently used are GET and POST methods.
⚫ post: We can use the post value of method attribute when we
want to process the sensitive data as it does not display the
submitted data in URL.
⚫ get: The get value of method attribute is default value while
submitting the form. But this is not secure as it displays data in
URL after submitting the form.
� target : Specify the target window or frame where the
result of the script will be displayed. It takes values like
_blank, _self, _parent,_top etc.
⚫ self: If we use _self as an attribute value, then the response will
display in current page only.
⚫ blank: If we use _blank as an attribute it will load the response
in a new page.
� name: it specifies a name used to identify the form.
METHODS OF FORM
�reset()-it is
used to reset a form .
Event name =onreset()
<input type=“button”>
or
<button type=“button”>...</button>
BUTTON OBJECT PROPERTIES
Reset object(button):
� It represents reset button in an HTML Form.
<input type=“reset”>
Submit object(button):
� It represents submit button in an HTML Form.
<input type=“text”>
� Example:
<textarea> . . . </textarea>
� Example:
<textarea name=“example1” cols=“50” rows=“50”>
</textarea>
CHECKBOX OBJECT
� The input element defines an input field. when you specify
type attribute as “checkbox "of this element then checkbox
is created.
� Checkbox object represents a checkbox in an HTML form.
� It allows the user to select one or more options from the
available choices.
� Syntax:
<select> … </select>
OPTION OBJECT
� Option object represents an HTML <option>
element.
� It is used to add items to a select element.
� Syntax:
function optionfruit(select)
{
var a = select.selectedIndex;
var fav = select.options[a].value;
if(a==0)
{
alert("Please select a fruit");
}
else
{
document.write("Your Favorite Fruit is <b>"+fav+".</b>");
}
}
</script>
</head>
<body>
<form>
List of Fruits:
<select name="fruit">
<option value="0">Select a Fruit</option>
<option value="Mango">Mango</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Strawberry">Strawberry</option>
<option value="Orange">Orange</option>
</select>
<input type="button" value="Select" onclick="optionfruit(this.form.fruit);">
</form>
</body>
</html>
OUTPUT
FORM EVENTS
� The change in the state of an object is known as
an Event. In HTML, there are various events which
represents that some activity is performed by the user
or by the browser.
� When JavaScript code is included in HTML, js react
over these events and allow the execution. This
process of reacting over the events is called Event
Handling. Thus, js handles the HTML events
via Event Handlers.
� For example, when a user clicks over the browser,
add js code, which will execute the task to be
performed on the event.
HTML EVENTS AND THEIR EVENT HANDLERS ARE
// Simple Program on onsubmit() & onfocus() Event handler
<html>
<body>
<script>
function validateform()
{
var uname=document.myform.name.value;
var upassword=document.myform.password.value;
if (uname==null || uname=="")
{
alert("Name cannot be left blank");
return false;
}
else if(upassword.length<6)
{
alert("Password must be at least 6 characters long.");
return false;
}
}
function emailvalidation()
{
var a=document.myform.email.value
if (a.indexOf("@")==-1)
{
<html>
<body>
<script type="text/javascript">
alert("You are a Valid User !!!");
</script>
</body>
</html>
OUTPUT:
MOUSEOVER EVENT AND MOUSEOUT EVENT
<html>
<body>
<p onmouseover=“over()” onmouseout=“out()”>THIS IS MOUSE
EVENT</p>
<p>The function over() is triggered when the user moves the mouse
pointer over the text</p>
<p>The function out() is triggered when the mouse pointer is moved
out of the text</p>
<script>
function over()
{
alert(“mouseover");
}
function out()
{
alert(“mouseout");
}
</script>
KEYDOWN EVENT
<html>
<body>
<p>A function is triggered when the user is pressing
a key in the input field.</p>
<input type="text" onkeydown="myFunction()">
<script>
function myFunction()
{
alert("You pressed a key inside the input
field");
}
</script>
</body>
</html>
KEYUP EVENT
<html>
<body>
<p>A function is triggered when the user releases a key in the
input field. The function transforms the character to upper
case.</p>
Enter your name: <input type="text" id="fname"
onkeyup="myFunction()">
<script>
function myFunction()
{
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
KEYPRESS EVENT
<html>
<body>
<p>A function is triggered when the user is pressing a
key in the input field.</p>
<input type="text" onkeypress="myFunction()">
<script>
function myFunction()
{
alert("You pressed a key inside the input field");
}
</script>
</body>
</html>
FORM OBJECTS AND ELEMENTS
� Document Object Model:
� The document object represents the whole html
document.
� When html document is loaded in the browser, it
becomes a document object.
� You can access a <form> element by using
getElementById():
� The document.getElementById() method returns
the element of specified id.
METHODS OF DOCUMENT OBJECT
� We can access and change the contents of document by
its methods.
� The important methods of document object are as
follows:
ACCESSING FIELD VALUE BY DOCUMENT OBJECT
� Here, we are using document.form1.name.value to
get the value of name field.
� Here, document is the root element that represents
the html document.
� form1 is the name of the form.
� name is the attribute name of the input text.
� value is the property, that returns the value of the
input text.
//WAP to find Cube of Number using getElementById
<html>
<body>
<script type="text/javascript">
function getcube()
{
var number=document.getElementById("number").value;
alert(number*number*number);
}
</script>
<form>
Enter No:<input type="text" id="number"
name="number"/><br/>
<input type="button" value="cube" onclick="getcube()"/>
</form>
</body>
</html>
CHANGING ATTRIBUTE VALUE DYNAMICALLY
� In JavaScript, we can change the attribute value of
any form elements dynamically. That means we can
assign new value to that attribute using function and
that function will be execute when event triggered.
� Steps:
</body>
</html>
OUTPUT:
EVALUATING CHECKBOX SELECTION:
� A checkbox is created by using the input element
with the type=“checkbox” attribute-value pair.
� A checkbox in a form has only two states(checked or
un-checked) and is independent of the state of other
checkboxes in the form. Check boxes can be grouped
together under a common name.
� You can write JavaScript function that evaluates
whether or not a check box was selected and then
processes the result according to the needs of your
application.
� Following example make use of five checkboxes to
provide five options to the user regarding fruit.
<html>
<head> <title> Html Forms</title>
<script language=“javascript”>
function selection()
{ var x ="You selected: ";
with(document.forms.myform)
{
if(a.checked == true)
{ x+= a.value+ " ";}
if(b.checked == true)
{ x+= b.value+ " "; }
if(o.checked == true)
{ x+= o.value+ " "; }
if(p.checked == true)
{ x+= p.value+ " "; }
if(g.checked == true)
{ x+= g.value+ " "; }
document.write(x); }
}
</script>
</head>
<body>
<form name="myform" action="" method=“post”>
Select Your Favourite Fruits:
<input type=“checkbox” name=“a” value=“Apple”>Apple
<input type=“checkbox” name=“b” value=“Banana”>Banana
<input type=“checkbox” name=“o” value=“Orange”>Orange
<input type=“checkbox” name=“p” value=“Pear”>Pear
<input type=“checkbox” name=“g” value=“Grapes”>Grapes
<input type=“reset” value=“Show” onclick=“selection()”>
</form>
</body>
</html>
OUTPUT:
� After click
1. isNaN()
� isNaN() method determines whether value of a variable is a
legal number or not.
� For example
document.write(isNan(0)); // false
document.write(isNan('JavaScript')); // true
2. eval()
� eval() is used to execute Javascript source code.
� It evaluates or executes the argument passed to it and generates
output.
� For example
eval("var number=2;number=number+2;document.write(number)");
//4
3. String()
� String() function converts the object argument
passed to it to a string value.
� For example
document.write(parseInt("45")); // 45
document.write(parseInt("85 days")); // 85
document.write(parseInt("this is 9")); // NaN
4.parseFloat()
� parseFloat() function takes a string as parameter
and parses it to a floating point number.
� For example
document.write(parseFloat("15.26")); // 15.26
document.write(parseFloat("15 48 65")); // 15
document.write(parseFloat("this is 29")); // NaN
document.write(pareFloat(" 54 ")); // 54
function enableBtn()
{
document.getElementById("myBtn").disabled = false;
}
</script>
</body>
</html>
OUTPUT:
After disabled After enable
READ ONLY ELEMENTS
� We can restrict the user from changing the value of
an element by setting its readOnly property to
true,if we want to enter a value in that element
then set readOnly property to false.
<html>
<head>
<script>
function readfun()
{
document.getElementById("t1").readOnly=true;
}
function writefun()
{
document.getElementById("t1").readOnly=false;
}
</script>
</head>
<body>
<form name="myform">
Username: <input type="text“ id="t1"/><br/> <br/>
<input type="button" value="Read-only" onclick="readfun()"/>
<input type="button" value="write" onclick="writefun()"/>
</form></body></html>