Chapter 3 CSS
Chapter 3 CSS
Form events
Event Description
onload Executes when the browser finishes loading a window or all frames within a
frameset
onunload Executes when the browser removes a document from a window or frame
onclick Executes when the mouse button is clicked over an element
ondblclick Executes when the mouse button is double-clicked over an element
onmousedown Executes when the mouse button is clicked while the mouse cursor is over an
element
onmouseup Executes when the mouse button is released while the mouse cursor is over an
element
onmouseover Executes when the mouse cursor moves onto an element
onmousemove Executes when the mouse cursor is moved while over an element
onmouseout Executes when the mouse cursor is moved away from an element
onfocus Executes when an element receives focus
onblur Executes when an element loses focus
onkeypress Executes when a key is pressed and released
onkeydown Executes when a key is held down.
onkeyup Executes when a key is released
onsubmit Executes when a form is submitted
onreset Executes when a form is reset
onselect Executes when text is selected in a textfield
onchange Executes when an element loses input focus and the value of the element has
changed since gaining focus
<html>
<head>
<title>Accessing form attributes</title>
<script language="Javascript" type="text/javascript">
function display()
{
alert('Value: ' + document.forms.order.Reset.value);
}
</script>
</head>
<body>
<FORM action="http://www.jimkeogh.com" method="post" name="order">
<P>
First Name: <INPUT type="text" name="Fname"/><BR>
Last Name: <INPUT type="text" name="Lname"/><BR>
Email: <INPUT type="text" name="Email"/><BR>
<INPUT name="Submit" value="Submit" type="submit"/>
<INPUT name="Reset" value="Reset" type="reset" onclick="display()"/>
</P>
</FORM>
</body>
</html>
Mouse events:
Javascripts intraction with HTML is handled through events that occur when the user or browser manipulates a
page
Mouse events :
1. mouseDown: Triggered by an element when a mouse button is pressed down over it.
2. MouseUP
3. MouseOver
4. MouseOut
5. Mouemove
Complex events:
1. Click
2. Contextmenu
3. Doubleclick
Example
<html
>
<head><title>Events</title>
<script>
function react()
{
alert("please enter any value");
}
</script></head>
<body>
<form name="myform" action="post"onmousedown="react()">
<input value="click mouse here">
</form>
</body>
</html>
Button events :
<html>
<head>
<title>EventHello.html</title>
<script type="text/javascript" src="EventHello.js">
</script>
<meta http-equiv="Content-Script-Type" content="text/javascript"/>
<script>
function sayHello(event)
{
window.alert("Hello World!\n\n"+"Event type:"+event.type+"\n"+"Event target
type:"+event.target.nodName);
return;
}
Example :
<html>
<head>
<title>Dynamically Changing Element Attributes</title>
<script language="Javascript" type="text/javascript">
function Highlight(Element)
{
Element.style.color = 'blue'
Element.style.backgroundColor = 'pink'
}
</script>
</head>
<body>
<FORM name="Contact" action="http://www.jimkeogh.com" method="post">
<P>
First Name: <INPUT value="Bob" type="text"
name="Fname" onchange="Highlight(this)"/><BR>
Last Name: <INPUT value="Smith" type="text"
Client Side Scripting Chapter 3 CO5G 5
name="Lname" onchange="Highlight(this)"/><BR>
Email: <INPUT value="bsmith@mcgrawhill.com" type="text"
name="Email" onchange="Highlight(this)"/><BR>
<INPUT name="Submit" value="submit" type="submit"/>
<INPUT name="Reset" value="Method" type="reset"/>
</P>
</FORM>
</body>
</html>
Example 2:
<html>
<head>
<title>HTML Form</title>
<script type="text/javascript">
function OnSelectionChange(element)
{
var selected=element.options[element.selectedIndex].value;
alert("You have selected:"+selected);
}
</script>
</head>
<body>
Select an item fromthe following list:</br>
<select onchange="OnSelectionChange(this)">
<option value="Apple">Apple
<option value="Banana">Banana
<option value="Orange">Orange
</selected>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> HTML Form </title>
<script language="Javascript" type="text/javascript">
Client Side Scripting Chapter 3 CO5G 6
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;
}
if(ElementValue==2)
{
OptionList[0].text = "Potato";
OptionList[0].value= 1;
OptionList[1].text = "Cabbage";
OptionList[1].value= 2;
}
}
}
</script>
</head>
<body>
<form name="myform" action="" method="post">
<p>
<select name="OptionList" size="2">
<option value=1>Mango
<option value=2>Banana
</select>
<br>
<input type="radio" name="group1" value=1 checked="true" onclick="updatelist(this.value)">Fruits
<input type="radio" name="group1" 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 Friuts:<br>
<input type="checkbox" name="a" value="apple">Apple<br>
<input type="checkbox" name="b" value="banna">Banana<br>
<input type="checkbox" name="o" value="orange">Orange<br>
<input type="checkbox" name="p" value="pear">Pear<br>
<input type="checkbox" name="g" value="grapes">Grapes<br>
<input type=reset value="Show" onclick="selection()">
</form>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>HTML Form</title>
<script language="Javascript" type="text/javascript">
function updateList(ElementValue)
{
with(document.forms.myform)
{
if(ElementValue == "fruits")
{
toggleButton.value = "vegetables";
OptionList[0].text = "Mango";
OptionList[0].value = 1;
OptionList[1].text = "Banana";
OptionList[1].value = 2;
}
if(ElementValue=="vegetables")
{
toggleButton.value = "fruits";
OptionList[0].text = "Potato";
OptionList[0].value = 1;
OptionList[1].text = "Cabbage";
OptionList[1].value = 2;
}
}
}
Client Side Scripting Chapter 3 CO5G 9
</script>
</head>
<body>
<form action="" name="myform" method="post">
<p>
<select name="OptionList" size="2">
<option value=1>Mango
<option value=2>Banana
</select>
<br>
<input type="reset" value="fruits" name="toggleButton"
onClick="updateList(this.value)">
</p>
</form>
</body>
</html>
<html>
<head>
<title> HTML Form </title>
<script language="Javascript" type="text/javascript">
function addEmail()
{
with(document.forms.myform)
{
if(Fname.value.length>0 && Lname.value.length>0)
{
//alert(Fname.value.charAt(0)+Lname.value+'@xyz.com');
Email.value=Fname.value.charAt(0)+Lname.value+'@xyz.com';
}
}
Client Side Scripting Chapter 3 CO5G 10
}
</script>
</head>
<body>
<form name="myform" action="post">
<p>
First Name:<input type="text" name="Fname"><br>
Last Name:<input type="text" name="Lname" ><br>
Email:<input type="email" name="Email" ><br>
<input name="Submit" value="submit" type="button" onclick="addEmail()">
</p>
</form>
</body>
</html>
<html>
<head>
<title>HTML Form </title>
</head>
<body>
<form name="myform" action="post">
<p>
First Name:<input type="text" name="Fname"><br>
Last Name:<input type="text" name="Lname"><br>
<img src="submit.jpg " onclick="javascript:document.forms.myform.submit()" width=100>
<img src="reset.jpg" onclick="javascript:document.forms.myform.reset()" width=100>
</p>
</form>
</body>
</html>
Example : The Email element is disabled until the first and last names are entered into
the form.
<html>
<head>
<title>Disabled</title>
<script language="Javascript" type="text/javascript">
function EnableEmail()
{
with (document.forms.Contact)
{
if (Fname.value.length >0 && Lname.value.length >0)
{
Email.disabled = false;
}
}
}
</script>
</head>
<body>
<FORM name="Contact" action="http://www.jimkeogh.com" method="post">
<P>
First Name: <INPUT type="text" name="Fname" onchange=" EnableEmail()"/> <BR>
Last Name: <INPUT type="text" name="Lname" onchange=" EnableEmail()"/><BR>
Email: <INPUT type="text" name="Email" disabled=true/><BR>
<INPUT name="Submit" value="Submit" type="submit"/>
<INPUT name="Reset" value="Reset" type="reset">
</P>
</FORM>
Example 1:
You can change the value of the readonly attribute from within your JavaScript function.
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript">
function changeState()
{
var textarea = document.getElementById("myText");
textarea.readOnly = !textarea.readOnly;
}
</script>
</head>
<body>
<textarea id="myText" rows="3" readOnly="readOnly">Change this text!</textarea>
<button onclick="changeState();">Change me</button>
</body>
</html>
Example 2:
The Email element is set to readonly. This means that the user cannot enter an e-mail address. Each
time the value of the Fname and Lname elements change, the SetEmail() function is called.
<html>
<head>
<title>Read Only</title>
<script language="Javascript" type="text/javascript">
function SetEmail()
{
with (document.forms.Contact)
{
if (Fname.value.length >0 && Lname.value.length >0)
{
Email.readonly = false;
Client Side Scripting Chapter 3 CO5G 13
Email.value = Fname.value.charAt(0) + Lname.value + '@mcgrawhill.com';
Email.readonly = true;
}
}
}
</script>
</head>
<body>
<FORM name="Contact"
action="http://www.jimkeogh.com" method="post">
<P>
First Name: <INPUT type="text" name="Fname" onchange="SetEmail()"/><BR>
Last Name: <INPUT type="text" name="Lname" onchange="SetEmail()"/><BR>
Email: <INPUT type="text" name="Email" readonly=true/><BR>
<INPUT name="Submit" value="Submit" type="submit"/>
<INPUT name="Reset" value="Reset" type="reset">
</P>
</FORM>
</body>