Professional Documents
Culture Documents
3.1 Building blocks of form, properties and methods of form, button, text, textarea,
checkbox, radio button, select element.
Form is used by commercial websites to collect data. Form is created using HTML form elements such as
textbox, button, checkbox, textarea, etc. JavaScript is used to make a web page interactive. Specifically, for
data validation and for dynamically interacting with form elements, JavaScript is used.
Forms are used to collect information. Data entered into a form is sent to the web server for processing,
when user clicks a submit button. The program that processes the form is called a Common Gateway
Interface (CGI) program. These programs are written in programming language such as JSP. CGI programs
interact with non-web application such as database and other systems that process the form data. After
processing, CGI creates another web page dynamically and sends it to browser.
Attributes:
action= “ URL” : It specify name of the web page or destination where the information will be
transferred after submitting the form.
method= “get/post” : It specify how the information is transferred from form to its destination. In get
method information is passed as a part of URL whereas in post method information is not visible in URL
but sent directly to the destination.
Example: <form action=”page2.html” method=”post”> … </form>
2. Textbox : A text box is used to enter data. It is a single line input on a web page.
Tag : <input type=“text”> : It is used to display a text box on a web page.
3. Password : A password box is used to enter data that appears in the form of special characters on a web
page inside box. Password box looks like a text box on a wab page.
Tag: <input type=“password”> : It is used to display a password box on a web page.
Attributes:
name=“ text” : Specify name of the element for unique identification.
cols=number : Specify width of the text area.
rows=number : Specify height of the text area.
readonly : Specify a text area as read only element.
Example: <textarea name=“t1” cols=10 rows=10>Enter your suggestions</textarea>
5. Checkbox : Checkbox elements are used to display multiple options from which user can select one or
more options. When a checkbox is selected by user, a tickmark ( √ ) symbol appears inside box.
Tag: <input type=“checkbox”> : It is used to display a checkbox on a web page.
6. Radio / option button : Radio buttons are used to display multiple options from which user can select
only one option. When a radio button is selected by user, a dot symbol appears inside button. Multiple
option buttons are group together to allow user to select only one option from the group. A group can be
created by giving same name to all option buttons in that group.
Tag : <input type=“radio”> : It is used to display a radio button on a web page.
7. Select element : <select> … </select> : This tag is used to create a drop-down list box or scrolling list
box from which user can select one or more options.
Attributes:
name=“ text” : Specify name of the element for unique identification.
size=number : Specify number of options visible in a list box on a web page.
Multiple : Allow user to select multiple option with control key.
9. Submit button : Submit button is used to display a command button which user can click on web page to
submit information entered in a form.
Tag : <input type=“submit”> : It is used to display a submit button on a web page.
10. Reset button : Reset button is used to clear all elements with their original state after user clicks on it.
Tag : <input type=“reset”> : It is used to display a reset button on a web page.
Output:
onunload event : This event occurs when a browser removes a document from a window or frame. It is used
with opening body tag.
Example: <body onunload=”function( ) ”></form>
onsubmit event : This event occurs when a form is submitted on a web page. It is used with opening form
tag.
Example : <form onsubmit=”function( )”></form>
onreset event : This event executes when a form is reset. It is used with opening form tag.
Example : <form onreset=”function( )”></form>
onselect event : This event executes when a text is selected from a text field. It is used with form elements.
Example : <input type=”text” onselect=”function( )”>
onchange event : This event executes when an element looses input focus and the value of the elements has
changed. It is used with form elements.
Example : <input type=”text” onchange=”function( )”>
onfocus event : This event executes when an element receives focus ( when an event becomes active).
Example: <input type=”text” onfocus=”function( )”>
onblur event : This event executes when an element looses focus (when control passes from one element to
another element.
Example : <input type=”text” onblur=”function( )”>
Mouse events:
onclick event : This event occurs when a mouse button is click on or over a form element.
Example : <input type=”text” onclick=”function( )”>
ondblclick event : This event occurs when a mouse button is double click on or over a form element.
Example : <input type=”text” ondblclick=”function( )”>
onmousedown event : This event executes when a mouse button is clicked while cursor is over an element.
Example : <input type=”text” onmousedown=”function( )”>
onmouseup event : This event executes when a mouse button is released while the cursor is over an element.
Example : <input type=”text” onmouseup=”function( )”>
onmouseover event : This event executes when mouse cursor moves onto an element.
Example : <input type=”text” onmouseover=”function( )”>
onmousemove event : This event executes when mouse cursor is moved while over an element.
Example : <input type=”text” onmousemove=”function( )”>
onmouseout event : This event executes when mouse cursor is moved away from an element.
Example : <input type=”text” onmouseout=”function( )”>
Key events:
onkeypress event : This event executes when a key is pressed and released.
Example : <input type=”text” onkeypress=”function( )”>
onkeydown event : This event excutes when a key is hold down(pressed state).
Example : <input type=”text” onkeydown=”function( )”>
Example:
function highlight(element)
{
element.style.color=”blue”;
element.style.background=”Silver”;
}
In the above example, element represents a form control whose attribute values has to be changed
dynamically. Style property is used to change appearance of control.
Program:
<html> Output:
<head>
<script type="text/javascript"> Initial form:
function highlight(element)
{
element.style.color="blue";
element.style.background="silver";
}
</script>
</head>
<body>
<form name="userentry">
Enter First Name:<input type="text" name="fname" Change in attributes at run time:
value="abc" onchange="highlight(this)">
<br>Enter Last Name:<input type="text" name="lname"
value="xyz" onchange="highlight(this)">
<br>Email:<input type="text" name="email"
value="abc@xyz.com" onclick="highlight(this)">
<br><input type="submit" value="submit form">
<br><input type="reset" value="reset form">
</form>
</body>
</html>
In the above program, when user changes the contents of text box onchange event call highlight function.
This function is used to change attributes of text box dynamically. Background and text color are the
properties used in above program.
<body>
<form name="userentry" method="post">
<p>
Select semester:
Semester 5<input type="radio" name="subjects"
value="1" onclick="displayitem(this.value)"
checked="true">
Semester 6<input type="radio" name="subjects"
value="2" onclick="displayitem(this.value)">
<br>Subject list: <br>
<select name="optionlist" size=3>
<option>CSS</option>
<option>OPS</option>
<option>AJP</option>
</select>
<input type="submit" value="Submit form"
onclick="feedback()">
<input type="reset" value="Reset form">
</form>
</body>
</html>
.
3.6 Evaluating checkbox selection:
Checkbox is used in the HTML form to allow user to select one or more items from a set of displayed items.
A JavaScript function is used to evaluate selection of checkbox and then process the application.
Program:
<html>
<head>
<script type="text/javascript">
function displayyear()
{
var text1=" You are in"
with(window.document.forms.userentry)
{
if(chk1.checked==true)
{
text1+="first"
}
if(chk2.checked==true)
{
text1+="second"
}
if(chk3.checked==true)
{
text1+="third"
}
disp.value=text1
}
}
</script>
</head>
<body>
<form name="userentry" method="post">
<p>
<input type="checkbox" name="chk1" value="First"> First Year<br>
<input type="checkbox" name="chk2" value="Second"> Second Year<br>
<input type="checkbox" name="chk3" value="Third"> Third Year<br>
<input type="text" name="disp" onfocus="displayyear()">
<input type="submit" >
</form>
</body>
</html>
3.7 changing a label dynamically:
A text/value displayed on button / element can be changed dynamically using JavaScript function.
Example : for example, when option list displays sem 5 subjects then course label placed on web page displays
semester five where as when it displays sem 6 subjects then course label displays semester six.
Syntax :
For submit function : - javascript:document.forms.form_name.submit()
For reset function : - javascript:document.forms.form_name.reset()
Programs:
<html>
<head>
</head>
<body>
<form name="f1" action="page1.html">
Enter username: <input type="text" name="username"><br>
Enter password: <input type="text" name="pwd"><br><br>
<img src="s1.jpg" width="50" height="50" onclick="javascript:document.forms.f1.submit()">
<img src="s2.png" width="50" height="50" onclick="javascript:document.forms.f1.reset()">
</form>
</body>
</html>
The above program will display two text boxes and two images on a web page. First image will work as
submit button and second image will work as reset button.
Disabling elements:
Disabling elements means preventing an user from entering data into it. A disabled element is visible on
form but not in active state. User can not use it at run time. A JavaScript function can be used to enable or
disable elements on form at run time.
An element can be disabled or enabled by setting the value of disabled attribute. Initially all elements are
enabled but by setting disabled property to true , an element can be disabled.
For example,
<input type=”text” name=”email” disabled=”true”>
The above statement shows email field on web page but it will be disabled.
Program:
In the following program, initially email field is disables. When user enters first and last name then only
email field becomes enabled.
<html>
<head>
<script type="text/javascript">
function enableemail()
{
with(document.forms.userentry)
{
if(fname.value.length>0 && lname.value.length>0)
email.disabled=false;
email.focus();
}
}
</script>
</head>
<body>
<form name="userentry" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname" onblur="enableemail()"><br>
Email:<input type="text" name="email" disabled="True">
<br><input type="submit" value="Submit"><br>
<input type="reset" value="Reset">
</form>
</body>
</html>
</form>
</body>
</html>
Output:
Read Only Element:
A JavaScript function can be used to make an element read only i.e. user can not change data from that field.
By using readonly attribute of an element, an element can be shown as readonly. If the read only attribute
value is set to true then no one can change the value of an element on web page.
Example:
<input type=”text” name=”email” readonly=”true”>
Program:
<html>
<head>
<script type="text/javascript">
function setemail()
{
with(document.forms.userentry)
{
if(fname.value.length>0 && lname.value.length>0)
{
email.value=fname.value.charAt(0)+lname.value+'@gmail.com';
}
}
}
</script>
</head>
<body>
<form name="userentry" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname" onblur="setemail()"><br>
Email:<input type="text" name="email" disabled="True">
<br><input type="submit" value="Submit"><br>
<input type="reset" value="Reset">
</form>
</body>
</html>
Output: