Chapter 3 Form and Event Handling
Chapter 3 Form and Event Handling
• The size of a text area can be specified by the cols and rows
attributes, or even better
• Ex:
• <textarea name=“ta1” rows=“2” cols=“30”>
This is a TextArea.
</textarea>
It creates a textarea called “ta1” with 2 rows and 30 cols. The text
between the tags <textarea></textarea> is displayed as a default initial
value of it.
TextArea:
• These are defined with their own tag <textarea> and
represented by textarea object.
• Textarea allows user to enter multiline data which is not
possible with textfield.
• A text area can hold an unlimited number of characters,
and the text renders in a fixed-width font (usually
Courier).
• The size of a text area can be specified by the cols and
rows attributes.
• If not mentioned default row value is “2” and cols value
is “20”.
TextArea:
• Ex:
<textarea name=“ta1” rows=“2” cols=“30”>
This is a TextArea.
</textarea>
It creates a textarea called “ta1” with 2 rows and 30 cols.
The text between the tags <textarea></textarea> is displayed
as a default initial value of it.
Button:
• Buttons use input tag and can be used with one of three
different types:
• type=“button”: It is a generic button. It performs no
action on its own, but you can assign it one using a
javascript event hndler.
• type=“submit”: It is a submit button. It causes the data in
the form fields to be sent to script.
• type=“reset”: It is a reset button. It sets all the form fields
back to their default value or blank.
• Ex: <input type="button">
<input type=“submit">
<input type=“reset">
Check box:
• It is a form element that looks like a small box.
• Clicking on it switches between the checked and unchecked
states.
• Checkboxes let a user select ZERO or MORE options of a
limited number of choices.
• Syntax:
<input type="checkbox">
• Ex:
<form>
<input type="checkbox" name=“lang" value=“C++"> C++
</form>
Radio Button:
• Another element for decisions is the radio button.
• These are similar to check boxes, but they exist in groups and
only one button can be checked in the group.
• Syntax:
<input type=“radio">
• Ex:
<form>
<input type=“radio" name=“r1" value=“Option1"> O1
<input type=“radio" name=“r1" value=“Option2"> O2
</form>
Drop Down List:
• It is useful for multiple choice selections.
• The <select> HTML tag is used to define selection list or drop
down list of text items.
• The content between the opening <option> and closing </option>
tags is what the browsers will display in a drop-down list.
• Ex:
<form >
<select name="color">
<option value=“Red">Red</option>
<option value=“Green">Green</option>
<option value=“Blue">Blue</option>
</select>
</form>
Input Type Email:
• It is used for input fields that should contain an e-mail address.
• Depending on browser support, the e-mail address can be
automatically validated when submitted.
• Some smartphones recognize the email type, and add ".com"
to the keyboard to match email input.
• Ex:
<form>
E-mail:
<input type="email" name="email">
</form>
Input Events/Form Events:
• There are following types of Form Events:
onchange: Occurs when element changes
onselect: Occurs when element is selected
onblur: Occurs when element loses focus
onfocus: Occurs when element gets focus
onsubmit: Occurs when user clicks submit button
onreset: Occurs when user clicks reset button
Key Events:
onkeydown: Occurs when user is pressing/holding down key
onkeypress: Occurs when user presses a key
onkeyup: Occurs when user releases a key
The onkeypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT,
ESC) in all browsers. To detect only whether the user has pressed a key,
use the onkeydown event instead, because it works for all keys.
Mouse Events:
• There are following types of Mouse Events:
onmouseover/onmouseout: When mouse passes over an element
onmousedown/onmouseup: When pressing/releasing a mouse
button
onmousedown: When mouse is clicked
onmousemove/onmouseout: When moving a mouse pointer
onclick: When mouse is clicked
ondblclick: When a mouse is double clicked
document.getElementById() method:
• The document.getElementById() method returns the element of specified
id.
• We can use document.getElementById() method to get value of any field.
But we need to define id for the input field.
• Ex:
<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>
document.getElementsByName() method:
• The document.getElementsByName() method returns all the element of
specified name.
• Syntax: document.getElementsByName("name")
• Ex:
<script type="text/javascript">
function totalelements() {
var allgenders=document.getElementsByName("gender");
alert("Total Genders:"+allgenders.length);
}
</script>
<form>
Male:<input type="radio" name="gender" value="male">
Female:<input type="radio" name="gender" value="female">
<input type="button" onclick="totalelements()" value="Total Genders">
</form>
Form Objects:
• Webpage contains various elements including Window as first
element.
• Window contains Document Object
• The objects are represented in following hierarchical order:
• Window Object: Present at the top of hierarchy
• Document Object: Every HTML document which loads into a
window is document object that contains page elements.
• Form Object: All tags enclosed in <form> </form> sets form
object
• Form Control Elements: All controls like text fields, button,
checkbox, radio button etc.
Changing Attribute values Dynamically:
• In javascript value of any attribute can be changed
dynamically.
• To achieve this onchange event is used as it executes a script
when user changes a value of an element.
Changing option list Dynamically:
• In javascript values can be change at runtime according to user
input.
• Option list is used to show list of items where user can select
any one.
• You can change the option list’s value according to choice or
user input.
Evaluating Checkbox Selection:
• The checked property sets or returns the checked state of a
checkbox.
• This property reflects the HTML checked attribute.
• We can use a function to determine which checkbox is
selected or whether selected or not.
Changing attribute value dynamically
• The setAttribute() method adds the specified attribute to
an element, and gives it the specified value.
• Math.PI // returns PI
• Math.SQRT2 // returns the square root of 2
• Math.SQRT1_2 // returns the square root of ½
• Math.LN2 // returns the natural logarithm of 2
• Math.LN10 // returns the natural logarithm of 104
<html>
<head>
<title> Using math object</title>
<script language="javascript">
function findmax(t1,t2)
{ alert("Maximum number is :" + Math.max(t1,t2)); }
function findmin(t1,t2)
{ alert("Minimum number is :" + Math.min(t1,t2)); }
function squareroot(t1)
{ alert(Math.sqrt(t1)); }
function power(t1)
{
t2=prompt("Enter power:");
alert("Power of " + t1 +"^" + t2 + "=" + Math.pow(t1,t2));
}
</script>
</head>
</html>
Intrinsic JavaScript Functions:
• JavaScript provides the intrinsic functions for Submit and
Reset button.
• One can use these functionalities while submitting and
resetting the form fields.
• The submit() method of form object can be used to send
the form to the server similar to same way when user
presses the submit button.
Disabling Elements:
• The disabled attribute is a boolean attribute.
• When present, it specifies that the element should be disabled.
• A disabled element is unusable.
• The disabled attribute can be set to keep a user from using the
element until some other condition has been met (like selecting
a checkbox, etc.).
• Then, a JavaScript could remove the disabled value, and make
the element usable.
Disabling Elements Cont.:
• The disabled attribute can be used on the following elements:
<button>
<fieldset>
<input>
<optgroup>
<option>
<select>
<textarea>
Read Only Elements:
• The readOnly property sets or returns whether a text field is read-
only, or not.
• A read-only field cannot be modified. However, a user can tab to it,
highlight it, and copy the text from it.
• Tip: To prevent the user from interacting with the field, use the
disabled property instead.
• Syntax:
Return the readOnly property:
textObject.readOnly
Set the readOnly property:
textObject.readOnly = true|false
<html>
<body>
<button id=“b1">Click</button>
<button onclick=“fun1()">Check</button>
<script>
function myFunction()
{
document.getElementById(“b1").disabled = true;
}
</script>
</body>
</html>
readonly Elements Ex:
<html>
<body>