Unit 3_41661511_2024_10_24_08_54
Unit 3_41661511_2024_10_24_08_54
A form is a section of a document which contains controls such as text fields, password fields,
checkboxes, radio buttons, submit button, menus etc.
An HTML form facilitates the user to enter data that is to be sent to the server for processing
such as name, email address, password, phone number, etc. .
We use HTML form element in order to create the JavaScript form.
Typical form control objects -- also called "widgets" -- include the following:
Text box for entering a line of text
Push button for selecting an action
Radio buttons for making one selection among a group of options
Check boxes for selecting or deselecting a single, independent option
Output:
Defines and names the form. Elsewhere in the JavaScript you can reference this form by
the name myform.
action=""
The action attribute defines the action that the browser will take to tackle the form
when it is submitted. Here, we have taken no action.
The action attribute specifies a URL that will process the form submission.
As this example is not designed to submit anything, the URL for the CGI program is
omitted.
method="get"
It defines the method data is passed to the server when the form is submitted.
input type="text" defines the text box object. This is standard HTML markup.
input type="button" defines the button object. This is standard HTML markup except for
the onClick handler.
onclick="fun( )" is an event handler -- it handles an event, in this case clicking the
button. When the button is clicked, JavaScript executes the expression within the
quotes. It calls function fun().
Tip: If the action attribute is omitted, the action is set to the current page.
Notes on POST:
Appends the form data inside the body of the HTTP request (the submitted form data
is not shown in the URL)
POST has no size limitations, and can be used to send large amounts of data.
Form submissions with POST cannot be bookmarked.
4. onsubmit
The purpose of the HTML onsubmit attribute is to execute the code specified, when the
associated form is submitted. HTML onsubmit attribute supports form element. There is no
default value of HTML onsubmit attribute.
Syntax
<form onsubmit="value" >.....</form>
Example:
<form name="myform" action="" method="get" onsubmit="alert('Form is submitted')">
<head>
<title></title>
<script type="text/javascript">
function fun()
{
alert("Hello, Your form is submitted")
}
</script>
</head>
<body>
<form name="Myform" action="" method="get" onsubmit="fun()">
Enter Something in Textbox
<input type="text" name="ID">
<input type="submit" name="button" value="Submit">
</form>
</body>
1. <input type="text">:
<input> element of type "text" are used to define a single-line input text field.
Example:
<!DOCTYPE html>
<html>
<body>
<h3>Input "text" type:</h3>
<form>
<label>Enter first name</label><br>
<input type="text" name="firstname"><br><br>
<label>Enter last name</label><br>
<input type="text" name="lastname"><br>
<p>Note:The default maximum character lenght is 20 </p>
</form>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<body>
<h2>This program is of Textarea</h2>
<form>
<label>Enter Full Name</label>
<input type="text" name="Fname"><br><br>
<label>Enter Comments</label>
<textarea name="Coomments" rows="10" cols="50"></textarea>
</form>
</body>
</html>
Output:
3. <input type="button">:
The <input> type "button" defines a simple push button, which can be programmed to control a
functionally on any event such as, click event.
Example:
<!DOCTYPE html>
<html>
<body>
<h2>The button Element</h2>
<input type="button" value= "ClickMe!" onclick="alert('Hello World!')">
</body>
</html>
Output:
Output:
After clicking on submit button, this will submit the form to server and will redirect the page
to action value.
5. <input type="radio">:
The <input> type "radio" defines the radio buttons, which allow choosing an option between a
set of related options. At a time only one radio button option can be selected.
The radio group must have share the same name to be treated as a group.
Using input type="radio" we can place radio button on the web page.
We can create a group of some radio button component.
Note: The value attribute defines the unique value associated with each radio button. The value
is not shown to the user, but is the value that is sent to the server on "submit" to identify which
radio button that was selected.
Output:
Checked attribute
A Boolean attribute which, if present, indicates that this radio button is the default
selected one in the group.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Checkboxes</h2>
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label><br><br>
<input type="submit" value="Submit" onclick="alert('form submitted')">
</form>
</body>
</html>
Output:
checked attribute
If we want to select any checkbox by default, then we have to set the checked attribute with the
"yes" value as described in the following syntax:
<input type="checkbox" name="field name" value="Initial value" checked="yes">
<!DOCTYPE html>
<html>
<body>
<h1>The select element</h1>
<p>The select element is used to create a drop-down list.</p>
<form>
<label>Choose a car:</label>
<select name="cars" id="cars">
<option value="Maruti">Maruti</option>
<option value="BMW">BMW</option>
<option value="Lexus">Lexus</option>
<option value="Audi">Audi</option>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
size Attribute
The size attribute specifies the number of visible options in a drop-down list.
If the value of the size attribute is greater than 1, but lower than the total number of options in
the list, the browser will add a scroll bar to indicate that there are more options to view.
Eg:-
8. <input type="email">
The <input type="email"> defines a field for an e-mail address.
The input value is automatically validated to ensure it is a properly formatted e-mail address.
To define an e-mail field that allows multiple e-mail addresses, add the "multiple" attribute.
Validation
Browsers that support the input type email provide validation support in different ways.
However, all browsers use a common validation algorithm following this pattern:
[email protected]
the minimum requirements are the @ and . symbols and a lack of spaces between the
characters.
9. <input type="password">
<input> elements of type password provide a way for the user to securely enter a password.
The element is presented as a one-line plain text editor control in which the text is masked so
that it cannot be read, usually by replacing each character with a symbol such as the asterisk
("*") or a dot ("•").
</head>
<body>
<form>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label for="pwd">Password:</label>
<input type="password" id="pwd" name="pwd" minlength="6" maxlength="8"><br><br>
<input type="submit" value="SignIn" >
</form>
</body>
</html>
<form>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email"><br><br>
Syntax:
<label> form_content... </label>
for attribute
The for attribute specifies which form element a label is bound to.
The for attribute of <label> must be equal to the id attribute of the related element to bind them
together.
Example
<form>
<h3>Select your gender</h3>
<input type="radio" id="male" name="gender" >
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender">
<label for="other">Other</label>
</form>
<label for="T5">Address</label><br>
<textarea name="Address" id="T5" rows="5" cols="10"></textarea><br><br>
Syntax
getElementById(T1)
Example 1:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to change the color of button.</p>
<input type="button" id="b1" value="click" onclick="myFunction()">
<script>
function myFunction()
{
document.getElementById("b1").style.background = "red";
}
</script>
</body>
</html>
Output:
Example 2
<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>
Each HTML element has an innerHTML property that defines both the HTML code and the text
that occurs between that element’s opening and closing tag.
By changing element’s innerHTML after some user interaction, we can make much more
interactive pages.
For this, we must give some id to the element and then place that id in getElementById
function.
Example :
<!DOCTYPE html>
<html>
<body>
<form>
<p id="demo">Click the button to change the text in this paragraph.</p>
<input type="button" name="Try it" onclick="myFunction()" value="Try it">
</form>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
Output:
Event Handler:
Each available event has an event handler, which is a block of code (usually a JavaScript
function) that runs when the event fires.
Execution of appropriate code on occurrence of event is called event handling.
Form Events:
Events that occur when a user interacts with an HTML form are called JavaScript form events
1. onfocus
2. onchange
3. onblur
4. onselect
5. onsubmit
6. onreset
1. onfocus
The onfocus event occurs when an element gets focus.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function myFunction()
{
alert('Use Capital Letters');
}
</script>
<label for "T1"> Enter Name</label>
<input type="text" name="T1" id="T1" onfocus="myFunction()">
</body>
</html>
Example 2:
<!DOCTYPE html>
<html>
<body>
Output:
2. onselect
Execute a JavaScript after some text has been selected in an <input> element:
Supported HTML tags: <input type="file">, <input type="password">, <input
type="text">, and <textarea>
Example:
<!DOCTYPE html>
<html>
<body>
Some text:
<input type="text" value="Select Some Text" onselect="myFunction()">
<script>
function myFunction()
{
alert("You have selected some text!");
}
</script>
</body>
</html>
Output
<p>Modify the text in the input field, then click outside the field to fire the onchange event.
</p>
<script>
function myFunction(val)
{
alert("The input value has changed. The new value is: " + val);
}
</script>
</body>
</html>
Output:
Example 2:
<!DOCTYPE html>
<html>
<body>
<p>This example demonstrates how to assign an "onchange" event to an input element.</p>
Enter your name:
<input type="text" id="fname" onchange="myFunction()">
<p>When you leave the input field, a function is triggered which transforms the input text to
upper case.</p>
<script>
function myFunction()
{
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
4. onblur
The onblur event occurs when an object loses focus.
The onblur event is the opposite of the onfocus event.
<!DOCTYPE html>
<html>
<body>
<p>Write something in the input field, and then click outside the field to lose focus (blur).</p>
<input type="text" id="fname" onblur="myFunction()" >
<script>
function myFunction()
{
alert("Input field lost focus.");
}
</script>
</body>
</html>
5. onsubmit
The onsubmit attribute fires when a form is submitted.
<!DOCTYPE html>
<html>
<body>
<p>When you submit the form, a function is triggered which alerts some text.</p>
<form onsubmit="myFunction()">
Enter name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
<script>
function myFunction() {
alert("The form was submitted");
}
</script>
</body>
</html>
<p>When you reset the form, a function is triggered which alerts some text.</p>
<form onreset="myFunction()">
Enter name: <input type="text">
<input type="reset">
</form>
<script>
function myFunction() {
alert("The form was reset");
}
</script>
</body>
</html>
<form onreset="function5()">
Enter name: <input type="text" id="T1" onfocus="function1()" onblur="function2()"
onchange="function3()" onselect="function4(this.value)">
<input type="reset">
</form>
<script>
function function1()
{
document.getElementById('T1').style.background="Gray";
}
function function2()
{
document.getElementById('T1').style.background="Cyan";
}
function function3()
{
var x = document.getElementById('T1');
x.value = x.value.toUpperCase();
}
function function4(value)
{
alert ("You have selected "+value)
}
function function5()
{
alert("You have reseted the data")
}
</script>
</body>
</html>
</head>
<body>
Output:
Output
<!DOCTYPE html>
<html>
<body>
<script>
function mouseDown()
{
document.getElementById("myP").style.color = "red";
}
function mouseUp()
{
document.getElementById("myP").style.color = "green";
}
</script>
</body>
</html>
Output:
<!DOCTYPE html>
<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>
Output:
2. onkeydown
This event occurs when the user has pressed down the key. It will occur even if the key pressed
does not produce a character value.
<!DOCTYPE html>
<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>
Output:
<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>
Output:
Example 1:
<!DOCTYPE html>
<html>
<body>
<p>Modify the text in the input field, then click outside the field to fire the onchange
event.</p>
<script>
function myFunction(val)
{
alert("The input value has changed. The new value is: " + val);
}
</script>
</body>
</html>
Output:
Q] Write a HTML script which displays 2 radio buttons to the users for fruits and
vegetables and 1 option list. When user select fruits radio button option list should
present only fruits names to the user & when user select vegetable radio button option
list should present only vegetable names to the user
<html>
<head>
<title>HTML Form</title>
<script language="javascript" type="text/javascript">
function updateList(ElementValue)
{
with(document.forms.myform)
{
if(ElementValue == 1)
{
optionList[0].text="Mango";
optionList[0].value=1;
optionList[1].text="Banana";
optionList[1].value=2;
optionList[2].text="Apple";
optionList[2].value=3;
}
if(ElementValue == 2)
{
optionList[0].text="Potato";
optionList[0].value=1;
optionList[1].text="Cabbage";
optionList[1].value=2;
optionList[2].text="Onion";
Output:
Output
<html>
<head>
<script language="Javascript" type="text/javascript">
function disable()
{
document.forms.myform.Fname.disabled=true;
}
function enable()
{
document.forms.myform.Fname.disabled=false;
}
</script>
</head>
<body>
<form name="myform" action="" method="post">
First Name: <input type="text" name="Fname">
<input type="button" name="b1" value="Disable" onclick="disable()">
<input type="button" name="b2" value="Enable" onclick="enable()">
</form>
</body>
</html>
OUTPUT
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function readonly()
{
document.forms.frm1.Fname.readOnly=true;
}
</script>
</head>
<body>
<form name="frm1">
Enter Name <input type="text" name="Fname">
<input type="button" name="b1" value= "Read Only" onclick="readonly()">
</form>
</body>
</html>
Output
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
Number()
Number() method takes an object as an argument and converts it to the corresponding number
value.
Return Nan (Not a Number) if the object passed cannot be converted to a number
document.write(Number(obj1)); // 123
document.write(Number(obj2)); // 0
document.write(Number(obj3)); // 1
String()
String() function converts the object argument passed to it to a string value.
parseInt()
parseInt() function takes string as a parameter and converts it to integer.
For example
document.write(parseInt("45")); // 45
document.write(parseInt("85 days")); // 85
document.write(parseInt("this is 9")); // NaN
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
An intrinsic function is often used to replace the Submit button and the Reset button with your
own graphical images, which are displayed on a form in place of these buttons.
<html>
<head>
<title>Using Intrinsic JavaScript Functions</title>
</head>
<body>
<FORM name="contact" action=" " method="post">
<P>
First Name: <INPUT type="text" name="Fname"/> <BR>
Last Name: <INPUT type="text" name="Lname"/><BR>
Email: <INPUT type="text" name="Email"/><BR>
<img src="submit.jpg" height="40" width="40"
onclick="javascript:document.forms.contact.submit()"/>
<img src="reset.jpg" height="40" width="40"
onclick="javascript:document.forms.contact.reset()"/>
</P>
</FORM>
</body>
</html>