CSS CH-3 Updated
CSS CH-3 Updated
Form
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
<body>
<form name="Myform" action="" method="get">
Enter Something in Textbox
<input type="text" name="ID">
<input type="button" name="button" value="Back" onclick="fun()">
</form>
</body>
</html>
Output:
form name="myform"
Defines and names the form. Elsewhere in the JavaScript you can reference this form by
the name myform.
action=""
The action tag 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 thebutton.
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 GET:
Appends the form data to the URL, in name/value pairs
NEVER use GET to send sensitive data! (the submitted form data is visible in the URL!)
The length of a URL is limited (2048 characters)
GET is good for non-secure data, like query strings in Google
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>Switch</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>
Output:
2. Textarea
The HTML <textarea> tag is used to define a multi-line text input control.
It can hold unlimited number of characters and the texts are displayed in a fixed-width font
(usually courier).
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:
4. <input type="submit">:
The <input> element of type "submit" defines a submit button to submit the form to the server when the "click"
event occurs.
Example:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<label>Enter User name</label><br>
<input type="text" name="firstname"><br>
<label>Enter Password</label><br>
<input type="Password" name="password"><br>
<br><input type="submit" value="submit">
</form>
</body>
</html>
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 at a time.
Using input type="radio" we can place radio button on the web page.
We can create a group of some radio button component.
<!DOCTYPE html>
<html>
<body>
<h2>Radio Buttons</h2>
<form>
<h3>Select your gender</h3>
<input type="radio" id="male" name="gender" >
<label>Male</label><br>
<input type="radio" id="female" name="gender">
<label>Female</label><br>
<input type="radio" id="other" name="gender">
<label>Other</label>
<br><br>
<h3>Select your favourite color</h3>
<input type="radio" id="green" name="color">
<label>Green</label><br>
<input type="radio" id="orange" name="color" >
<label>Orange</label><br>
<input type="radio" id="white" name="color" >
<label>White</label>
</form>
</body>
</html>
Output:
6. <input type="checkbox">:
The <input> type "checkbox" are displayed as square boxes which can be checked or unchecked to select the choices
from the given options.
Note: The "radio" buttons are similar to checkboxes, but there is an important difference
between both types: radio buttons allow the user to select only one option at a time, whereas
checkbox allows a user to select zero to multiple options at a time.
<!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:
7. Select element
The <select> element is used to create a drop-down list.
The <select> element is most often used in a form, to collect user input.
The name attribute is needed to reference the form data after the form is submitted (if you omit
the name attribute, no data from the drop-down list will be submitted).
The id attribute is needed to associate the drop-down list with a label.
The <option> tags inside the <select> element define the available options in the drop-down list.
<!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:
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.
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>
The <input type="reset"> defines a reset button which resets all form values to its initial
values.
<form>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email"><br><br>
Select Gender<br>
<input type="radio" name="gender" value="Male" id="R1">
<label for="R1">Male</label><br>
<input type="radio" name="gender" value="Female" id="R2">
<label for="R2">Female</label><br>
<input type="radio" name="gender" value="Other" id="R3">
<label for="R3">Other</label><br><br>
<label>Select Year</label>
<select name="Year" id="S1">
<option value="FY">FY</option>
<option value="SY">SY</option>
<option value="TY">TY</option>
</select><br><br>
<label>Address</label><br>
<textarea name="Address" rows="5" cols="20"></textarea><br><br>
<label for="E1">Eamil ID</label><br>
<input type="email" name="Email Id" id="E1"><br><br>
<label for="P1">Password</label><br>
<input type="Password" name="Password"><br><br>
<input type="Submit" name="Register" value="Register" onsubmit="alert('Form submitted
Successfully')">
<input type="reset" name="Resset" value="Reset" onclick="alert('Form Reset')">
</form>
</body>
</html>
getElementById ( ) Method
The getElementById() method returns the element that has the ID attribute with the specified
value.
This method is one of the most common methods in the HTML, and is used almost every time
you want to manipulate, or get info from, an element on your document.
Returns null if no elements with the specified ID exists.
An ID should be unique within a page. However, if more than one element with the specified ID
exists, the getElementById() method returns the first element in the source code.
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:
innerHTML property
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:
FORM EVENTS
Event is something that causes JavaScript to execute the code.
JavaScript's interaction with HTML is handled through events that occur when the user or the
browser manipulates a page.
When the page loads, it is called an event.
When the user clicks a button, that click too is an event.
Other examples include events like pressing any key, closing a window, resizing a window, etc.
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:
1. onfocus
2. onchange
3. onblur
4. onselect
1. onfocus
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function myFunction()
{
Example 2:
<!DOCTYPE html>
<html>
<body>
<p>When the input field gets focus, a function is triggered which changes the background-
color.</p>
Enter your name:
<input id="t1" type="text" onfocus="myFunction()">
<script>
function myFunction()
{
document.getElementById("t1").style.background = "yellow";
}
</script>
</body>
</html>
Output:
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
onchange
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:
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>
Output:
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>
<html>
<head>
<h1> Javascript Events </h1>
<h2 onmouseover="mouseoverevent()" onmouseout="onmouseoutevent()"> Keep cursor over
me</h2>
</head>
<body>
function mouseoverevent()
{
alert("Welcome to Spc");
}
function onmouseoutevent()
{
alert("Welcome Spc World");
}
</script>
</body>
</html>
Output:
<!DOCTYPE html>
<html>
<body>
<p>This example demonstrates how to assign red color when the mouse is over Register
button and assign Green color when the mouse is moved out from Register button </p>
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:
onmousemove
<html>
<head>
<script>
function sayHello() {
alert("You are moving the mouse")
}
</script>
</head>
<body>
<p> When you move cursor inside textarea, it gves alert</p>
<label>Comment</label>
<textarea name="Comment" onmousemove = "sayHello()">
</textarea>
</body>
</html>
Key Events
onkeyup
This event occurs when the user has released the key. It will occur even if the key released
does not produce a character value.
<!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:
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:
onkeypress
This event occurs when the user presses a key that produces a character value. These include
keys such as the alphabetic, numeric, and punctuation keys. Modifier keys such as ‘Shift’,
‘CapsLock’, ‘Ctrl’ etc. do not produce a character, therefore they have no ‘keypress’ event
attached to them.
<!DOCTYPE html>
<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:
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";
optionList[2].value=3;
}
}
}
</script>
</head>
<body>
<form name="myform" action="" method="post">
<p>
<select name="optionList">
<option value="1">Mango</option>
<option value="2">Banana</option>
<option value="3">Apple</option>
</select>
<br><br>
<input type="radio" name="grp1" value=1 checked="true"
onclick="updateList(this.value)">Fruits
<input type="radio" name="grp1" value=2
onclick="updateList(this.value)">Vegetables
<br>
<input name="Submit" value="Submit" type="submit">
</p>
</form>
</body>
</html>
Output:
Output
Disabling Elements
1. Disabled form fields or elements values don’t post to the server for processing.
2. Disabled form fields or elements don’t get focus.
3. Disabled form fields or elements are skipped while tab navigation.
<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