CSS Chapter 3Notes
CSS Chapter 3Notes
Unit Outcomes(UOs)
3a. Write JavaScript to design a form to accept input values for the given problem.
3b. Use JavaScript to implement form events to solve the given problem.
3c. Develop JavaScript to dynamically design specified attribute value to the given form control.
3d. Use the intrinsic function with specified parameters.
Contents
3.1 Building of blocks of a form, properties and methods of form, button, text, text area,
checkbox, radio button, select element.
3.2 Form Events – mouse events, key events.
3.3 Form objects and elements.
3.4 Changing attribute value dynamically.
3.5 Changing option list dynamically.
3.6 Evaluating checkbox selection.
3.7 Changing a label dynamically.
3.8 Manipulating form elements.
3.9 Intrinsic JavaScript functions, disabling elements, read only elements.
• Uses of Form
• Collect information from customer or from student through online registration.
• Forms are used for online Survey.
• Conducting online examination, for getting Feedback and so on.
• Information present in the form is submitted to server for further processing.
<html>
<head>
<title> My Page</title>
</head>
<body>
<form>
<label><br>Input String Text area:</br>
<textarea cols="40" rows="5" name="myname">
</textarea>
</form>
</body>
</html>
<html>
<head>
<title> My Page</title>
</head>
<body>
<form name="radioform">
<div align="left"><br>
<br> Select the fruits which you like the most</br>
<input type="radio" name="group1" value="Mango" >Mango</br>
<input type="radio" name="group1" value="Apple" >Apple</br>
<input type="radio" name="group1" value="Grapes" >Grapes</br>
</br>
<br> Select the flower which you like the most</br>
<input type="radio" name="group2" value="Rose" >Rose</br>
<input type="radio" name="group2" value="Lotus" >Lotus</br>
<input type="radio" name="group2" value="Jasmine" >Jasmine</br>
</form>
</body>
</html>
<html>
<head>
<title> My Page</title>
</head>
<body>
<form name="dropdownformform">
<div align="center">
<select name="My Menu">
<option value="Mango">Mango</option>
<option value="Banana">Banana</option>
<option value="Apple">Apple</option>
</select>
</form>
</body>
</html>
Create a HTML document that has the form with the following controls
a) A text box to collect the customer name
b) Four checkboxes, one for the following items:
i) Four HTML textbooks for Rs. 1000 ii) Eight XML textbooks for Rs. 2000
iii) Four Javabeans textbooks for Rs. 2500 iv) Eight UML textbooks for Rs. 1500
c) A Collection of radio button that are labelled as follows –
<html>
<body>
<center><h2>Registration Form</h2></center>
<hr/>
<form>
<table>
<tr>
<td>Name:</td>
<td><input type="text" size="25" value=""></td>
</tr>
<tr>
<td>Select the desired items:</td>
<td>
<select name="f">
<option value="4 HTML Books(Rs.1000)">4 HTML Books(Rs.1000)</option>
<option value="8 XML Books(Rs.2000)">8 XML Books(Rs.2000)</option>
<option value="4 JavaBeans Books(Rs.2500)">4 JavaBeans
Books(Rs.2500)</option>
<option value="8 UML Books(Rs.1500)">8 UML Books(Rs.1500)</option>
</select>
</td>
</tr>
<tr>
<td>
Mode of Payment:
</td>
</tr>
<tr>
<td><input type="radio" name="group1" value="Cash">Cash</td>
</tr>
<tr>
<td><input type="radio" name="group1" value="Cheque">Cheque</td>
</tr>
<tr>
<td><input type="radio" name="group1" value="Credit Card">Credit
Card</td>
</tr>
<tr></tr><tr></tr><tr></tr>
<td><input type="Submit" value="Submit"></td>
<td><input type="Reset" value="Reset"></td>
</body>
</html>
Write a form to collect details of a user such as name, address, radio button to choose subject of book he
wants to buy, dropdown to choose favorite author, comments for the last book he read
<html>
<head>
<title>My Page</title>
</head>
<body>
<form>
<b>Name:</b><input type="text" size="20" value=""></br>
<b>Address:</br><input type="text" size="35" value=""><br/>
<b>Subjects:</b></br>
<input type="radio" name="authors" value="web Programming">Web Programming</br>
<input type="radio" name="authors" value="Computer Networks">Computer
Networks</br>
<input type="radio" name="authors" value="Software Engineering">Software
Engineering</br>
<input type="radio" name="authors" value="Data Structures">Data Structures</br>
<b>Select favorite Author:</b>
<select name="MyMenu">
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
</select>
</br>
<b>Comments:</b></br>
<textarea cols="30" rows="10" name="Comments">
</textarea>
<br/><br/>
<input type="submit" value="Submit">
<input type="reset" value="Clear">
</form>
</body>
</html>
Design a simple HTML form for filing the information for registration of a student
<html>
<head>
<title>Registration form</title>
</head>
<body>
<form>
<b>Student Name :</b><input type="text" size="20" value=""></br></br>
<b>Address :</b><input type="text" size="20" value=""></br></br>
<b>Email ID :</b><input type="text" size="20" value=""></br></br>
<b>Password :</b><input type="text" size="20" value=""></br></br>
<b>Select Course:</b></br></br>
<input type="radio" name="courses" value="Computer Engg">Computer
Engg<br/><br/>
<input type="radio" name="courses" value="Mechanical Engg">Mechanical
Engg<br/><br/>
<input type="radio" name="courses" value="Civil Engg">Civil Engg<br/><br/>
<input type="radio" name="courses" value="Electrical Engg">Electrical
Engg<br/><br/>
<b>Select Payment Mode:</b>
<Select name="Mymenu">
<option value=Cheque">Cheque</option>
<option value=Cash">Cash</option>
<option value=Card">Card</option>
</select> <br/><br/><br/>
<input type="submit" value="Submit">
<input type="reset" value="Clear">
</form>
</body>
</html>
Write a form to make login and password
Write a form to make login and password
<html>
<head>
<title> Login Form Demo</title>
</head>
<body bgcolor="Red">
<Center>
<h2>Login Form</h2>
</center>
<form name="form1">
<table>
<tr>
<td><b>Name:</b></td>
<td><input type="text" name="username">
</tr>
<tr>
<td><b>Password:</b></td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>
<td><input type="reset" name="reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>
change onchange When the user modifies or changes the value of a form
element
mousedown onmousedown When the mouse button is pressed over the element
mouseup onmouseup When the mouse button is released over the element
Mouseover Event
<html>
<head>
<h1> Javascript Events </h1>
</head>
<body>
<script language="Javascript" type="text/Javascript">
<!--
function mouseoverevent()
{
alert("This is Mouse Over Event");
}
//-->
</script>
<p onmouseover="mouseoverevent()"> Keep cursor over me</p>
</body>
</html>
ondblclickevent: This event occurs when a mouse button is double clicked on or over a form element.
Example:
<input type="text" ondblclick=" function ()">
onmousedownevent: This event executes when a mouse button is clicked while cursor is over an element.
Example:
<input type="text" onmousedown=" function ()">
onmouseupevent: This event executes when a mouse button is released while the cursor is over an
element.
Example:
<input type="text" onmouseup=" function ()">
onmouseoverevent: This event executes when mouse cursor moves onto an element.
Example:
<input type="text" onmouseover=" function ()">
onmousemoveevent: This event executes when mouse cursor is moved while over an element.
Example:
<input type="text" onmousemove=" function ()">
onmouseoutevent: This event executes when mouse cursor is moved away from an element.
Example:
<input type="text" onmouseout=" function ()">
Insert an image into a wepage. Write a script which displays the message when the mouse is over the
image. The coordinates of the mouse should be displayed if click attempted on the image
<html>
<head>
<script type="text/javascipt">
function my_fun1()
{
points.innerText="Now the mouse is moving";
}
function my_fun2()
{
points.innerText="("+event.offsetX+","+event.offsetY+")";
}
</script>
</head>
<body onmousemove="my_fun1()" onmousedown="my_fun2()">
<center>
<span id="points">(0,0)</span>
<img src="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
style="position:absolute;top:50;left:90">
</center>
</body>
</html>
Keydown & onkeydown & When the user press and then
Keyup onkeyup release the key
Keyup Event
<html>
<head>
<title>Demo of key event Tag Attribute</title>
<script type="text/javascript">
function my_fun()
{
alert("you pressed the key");
}
</script>
</head>
<body>
<center>
<form>
Enter Name:<input type="text" onkeypress="my_fun()"/>
</form>
</center>
</body>
</html>
Keydown Event
<html>
<head> Javascript Events</head>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onkeydown="keydownevent()"/>
<script>
<!--
function keydownevent()
{
document.getElementById("input1");
alert("Pressed a key");
}
//-->
</script>
</body>
</html>
Write a JavaScript to modify the status bar using onmouseover and mouseout with
links.when the user moves his mouse over the link, it will display “MSBTE” in the
status bar. When the user moves his mouse away from the link the status bar will
display nothing.
<html>
<head>
<title>JavaScript Status Bar</title></head>
<body>
<a href=" https://msbte.org.in/"
onMouseOver="window.status='MSBTE';return true"
onMouseOut="window.status='';return true">
MSBTE
</a>
</body>
</html>
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 list should present
only fruits to the user & when user select vegetables radio button option list should
present only vegetables 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" size="2">
<option value=1>Mango
<option value=2>Banana
<option value=3>Apple
</select>
<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="Reset" value="Reset" type="reset">
</p>
</form>
</body>
</html>
<html>
<head>
<title>HTML Form</title>
<script language="javascript" type="text/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: <br>
<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>
</form>
</body>
</html>
Code:
<html>
<body>
<html>
<script type="text/javascript">
function modifyList(x)
{
with(document.forms.myform)
{
if(x ==1)
{
optionList[0].text="Green Tea";
optionList[0].value=1;
optionList[1].text="Milk Tea ";
optionList[1].value=2;
optionList[2].text="Black Tea";
optionList[2].value=3;
}
if(x ==2)
{
optionList[0].text="Capaccino";
optionList[0].value=1;
optionList[1].text="Latte";
optionList[1].value=2;
optionList[2].text="Expression";
optionList[2].value=3;
}
if(x ==3)
{
optionList[0].text="MAAZA";
optionList[0].value=1;
optionList[1].text="SPRITE";
optionList[1].value=2;
optionList[2].text="COKA-COLA";
optionList[2].value=3;
}
}
}
</script>
</head>
<body>
<form name="myform" action=" " method="post">
<table><tr><td>
Select Beverage: </td><td>
<input type="checkbox" name="grp1" value=1 checked="true"
onclick="modifyList(this.value)"> TEA
<input type="checkbox" name="grp1" value=2 onclick="modifyList(this.value)">
COFFEE
<input type="checkbox" name="grp1" value=3 onclick="modifyList(this.value)">
SOFT DRINK
</td></tr></table>
<select name="optionList" size="3">
<option value=1>Kiwi
<option value=1>Pine-Apple
<option value=1>Apple
</tr> </table>
</select>
</form>
</body>
</html>
Write a JavaScript to create rollover effect that involves text and images. When the user places his or her
mouse pointer over a book title, the corresponding book images appears
<html>
<head>
<title>
rollovers</title>
</head>
<body>
<table border="1" width="100%">
<tbody>
<tr valign="top">
<td width="50%">
<a><img height="500" src="motivation.png" width="900"
name="clr"></a></td>
<td><a onmouseover="document.clr.src='blue.png' ">
<b><u>Motivational book</u></b></a>
<br>
<a onmouseover="document.clr.src=education.png' ">
<b><u>Educational book</u></b></a>
<br>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Text:
Input "text" is an object to enter a single line of text whose content
will be part of form data.
In html a text is created by following code:
<input type="text" name="textname" id="textid" value="
assign_value" />
Example:
<script type="text/javascript">
function changeText()
{
var userInput = document.getElementById('userInput').value;
document.getElementById('vp').innerHTML = userInput;
}
</script>
<input type='text' id='userInput' value='Enter Text Here' />
<p>Welcome <b id='vp'>JavaScript</b></p>
<input type='button' onclick='changeText()' value='Change Text'/>
</script>
TextArea:
The Textarea object represents an HTML <textarea> element.
The <textarea> tag indicates a form field where the user can enter a large amount of text.
You can access a <textarea> element by using getElementById()
Example:
<html>
<body>
<textarea cols="30" rows="5" wrap="hard" readonly="yes"
disabled="yes">
As you can see many times word wrapping is often the desired look
for your textareas. Since it makes everything nice and easy to read
and preserves line breaks.
</textarea>
</body>
</html>
Checkbox:
<input> elements of type checkbox are rendered by default as boxes that are checked (ticked)
when activated. A checkbox allows you to select single values for submission in a form (or not).
Syntax for creating checkbox is:
<input type="checkbox" id="myCheck" onclick="myFunction()">
A checkbox can have only two states:
1. Checked
2. Unchecked
Example:
<html>
<body>
<div>
<br>
<input type="checkbox" name="program" id="it" value="IT">
<label for="it">Information Tech</label><br>
<input type="checkbox" name="program" id="co" value="CO"
checked>
<label for="co">Computer Engg</label><br>
<input type="checkbox" name="program" id="ej" value="EJ">
<label for="ej">Electronics</label><br>
<button onclick="validate();">Validate</button>
</div>
<div id="status">
</div>
<script>
function validate()
{
var elements = document.getElementsByName("program");
var statusText = " ";
for (var index=0;index <elements.length;index++)
{
statusText = statusText +
elements[index].value+"="+elements[index].checked+"<br>";
}
document.getElementById("status").innerHTML = statusText;
}
</script>
</body>
</html>
Select:
Form SELECT elements (<select>) within your form can be accessed and manipulated in JavaScript
via the corresponding Select object.
To access a SELECT element in JavaScript, use the syntax:
document.myform.selectname //where myform and selectname are names of your form/element.
document.myform.elements[i] //where i is the position of the select element within form
document.getElementById("selectid") //where "selectid" is the ID of the SELECT element on the
page.
Example:
<html>
<body>
<select id="programs" size="5">
<option>Computer Engineering</option>
<option>Information Technology</option>
<option>Chemical Engineering</option>
<option>Electronics &TeleComm.</option>
</select>
<p>Click the button to disable the third option (index 2) in the
dropdown list.</p>
<button onclick="myFunction()">Disable Option</button>
<script>
function myFunction()
{
var x = document.getElementById("programs").options[2].disabled
= true;
document.getElementById("programs").options[2].style.color =
"red";
}
</script>
</body>
</html>
Form:
A form is a section of an HTML document that contains elements such as radio buttons, text boxes
and option lists. HTML form elements are also known as controls.
Elements are used as an efficient way for a user to enter information into a form. Typical form
control objects also called "widgets"
includes 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.
The <form> element can contain one or more of the following form
elements:
· <input> · <textarea> · <button> · <select> · <option> · <fieldset> ·
<label>
· <legend>
Syntax:
<form name = "myform" id = "myform" action = "page.html"
onSubmit = "test()"> -----objects---- </form>