HTML Forms are used widely on the Internet to gather information from users or adminstrators accessing the web site. This information is typically entered into a database and later retrieved in other applications. Input for information such as a name, a choices of preferences, etc. are pervasive features. Whenever you type something into an HTML web page and submit the page, it is done through an HTML form.
The HTML Form is constructed using a form element, typically like this:<form action="a-web-server-side-script"> ... form components ... </form>
As suggested, the information entered is usually processed by a server-side program, i.e., a program running on the server where you accessed the HTML page. In contrast, JavaScript is a client-side program, namely, it runs in the browser.
The components within forms are these:<button onclick="">Push Me</button>
<input type="text" size="20" />
<input type="text" size="20" value="YOUR NAME" />
<input type="password" size="20"/>
<input type="checkbox" /> Would you like to receive our monthly newsletter?Would you like to receive our monthly newsletter?
<input type="checkbox" checked /> Would you like to receive our monthly newsletter?Would you like to receive our monthly newsletter?
<input type="radio" name="gender1" /> Male <input type="radio" name="gender1" /> FemaleMale Female
<input type="radio" name="gender2" checked /> Male <input type="radio" name="gender2" /> FemaleMale Female
Selection lists are created by the select element. They can appear as drop-down lists or scrolled lists of items. Each item in the list is specified by an option element and has a has a visible label and an underlying value. If the value is absent, it is taken to be the label.
In addition to the name attribute the key attributes are these:<select> <option value="1">dog</option> <option value="2">cat</option> <option value="3">cow</option> <option value="4">pig</option> </select>
<select size="3"> <option value="1">dog</option> <option value="2">cat</option> <option value="3">cow</option> <option value="4">pig</option> </select>
<textarea rows="5" cols="20"></textarea>
<textarea rows="5" cols="20">With some initial input</textarea>
<button onclick="alert('button pressed')">Press Me</button>
which behaves like this:
<button onclick="location='http://www.google.com'">Go to Google</button>Here is the effect:
window.locationwhich indicates the current URL displayed by the browser.
<img src="yahoo_out.gif"
onclick="location='http://www.yahoo.com'"
onmouseover="src='yahoo_over.gif'"
onmouseout="src='yahoo_out.gif'" />
with the following effect:
span.link {
color: blue;
text-decoration: none;
cursor: pointer; /* make mouse not look like "text cursor" */
}
Consider this usage:
Here is a <a href="http://www.google.com">real hyperlink to google</a> as others in this document. <br /><br /> Here is a <span class="link" onclick="location='http://www.google.com'" onmouseover="style.color='red';style.textDecoration='underline';window.status='http://www.google.com';" onmouseout="style.color='blue';style.textDecoration='none';window.status=''" >manufactured hyperlink to google</span> using a span element.The effect is this:
<input type="text" id="someid" />then within JavaScript, we can access the value within the textfield:
var someid = document.getElementById("someid")
and use it for reading or writing as in these examples:
var str = someid.value // read contents of textfieldor
someid.value = 33 // write 33 into the textfield
<html>
<head>
<title>Square in Textfields</title>
<script type="text/javascript">
<!--
function square()
{
var source = document.getElementById("source") // source textfield
var output = document.getElementById("output") // output textfield
var str = source.value // source value
var num = parseFloat(str) // string -> numeric
if (isNaN(num)) {
alert( "invalid entry" )
output.value = ""
} else {
var result = num * num // compute result
output.value = result
}
}
// -->
</script>
</head>
<body>
Input: <input type="text" id="source"/>
<button onclick="square()" >Square It</button>
<p>
Output: <input type="text" id="output" readonly />
</p>
</body>
</html>
<select id="someid" >
<option value="value1">option1</option>
<option value="value2">option2</option>
...
</select>
then within JavaScript, we can access the selected option's value:
var someid = document.getElementById("someid"); var selected_value = someid.value // someid.value is the selected value
<html>
<head>
<title>Select and Go</title>
<script type="text/javascript">
<!--
function gotosite()
{
var site = document.getElementById("site")
location = site.value // go to this site
}
// -->
</script>
</head>
<body>
Choose a search engine:
<select id="site">
<option value="http://www.google.com">Google</option>
<option value="http://www.yahoo.com">Yahoo</option>
<option value="http://www.dogpile.com">DogPile</option>
</select>
<button onclick="gotosite()">Go to site</button>
</body>
</html>
A key idea expressed in this example is the usage of the JavaScript object
window.locationwhich dictates the current URL displayed by the browser. This property can be reassigned to a new URL as we have done in this program, causing the browser to relocate to this site.
location = site.value;
<html>
<head>
<title>Select and Go on change</title>
<script type="text/javascript">
<!--
function gotosite()
{
var site = document.getElementById("site")
if (site.value != "NONE") {
location = site.value // go to this site
}
}
// -->
</script>
</head>
<body>
Choose a search engine:
<select id="site" onchange="gotosite()">
<option value="NONE">-----</option>
<option value="http://www.google.com">Google</option>
<option value="http://www.yahoo.com">Yahoo</option>
<option value="http://www.dogpile.com">DogPile</option>
</select>
</body>
</html>
Observe the alterations we've made. In particular, you need to add
a "no site" option, as the first selection, in this case:
<option value="NONE">-----</option>If we change to a real site, and go back, we'll see our site chose still selected. Therefore, if we then change to the "no site" choice with value NONE, we must recognize this fact and omit the location reassignment in the gotosite function:
if (site.value != "NONE") location = site.value; // go to this site
var myelt = document.getElementById( "myelt" ) myelt.disabled = true // disable it, or myelt.disabled = false // enable itThe following is a simple demo program illustrating the usage of boolean attributes: Here is the code:
<html>
<head>
<title>Boolean Attribute Demo</title>
<script>
function init() {
var cb = document.getElementById("cb")
var bt = document.getElementById("bt")
cb.checked = true
bt.disabled = true
}
window.onload = init
</script>
</head>
<body>
<h2>Boolean Attribute Demo</h2>
<input type="checkbox" id="cb" />
<button type="" onclick="alert('pressed')" id="bt">Button</button>
<input type="text" id="tf" value="some content" readonly />
<select>
<option value="a">A</option>
<option value="b" selected >B</option>
</select>
</body>
</html>
For checkboxes and radio buttons the checked attribute corresponds to the JavaScript property of the same name. This is a boolean value, i.e. true or false.
In particular, if, say we have a checkbox:<input type="checkbox" id="someid" />then, we can test whether this is checked or not in JavaScript:
var someid = document.getElementById("someid");
if (someid.checked) {
// do something based on the box being checked
}
Here are some examples which illustrates the point.
<html>
<head>
<title>Check Test</title>
<script type="text/javascript">
<!--
function check_test()
{
var c = document.getElementById("c")
var r1 = document.getElementById("r1")
var r2 = document.getElementById("r2")
var cinfo = document.getElementById("cinfo")
var r1info = document.getElementById("r1info")
var r2info = document.getElementById("r2info")
if (c.checked)
cinfo.innerHTML = "yes"
else
cinfo.innerHTML = "no"
if (r1.checked)
r1info.innerHTML = "yes"
else
r1info.innerHTML = "no"
if (r2.checked)
r2info.innerHTML = "yes"
else
r2info.innerHTML = "no"
}
// -->
</script>
</head>
<body>
<input type="checkbox" id="c" onclick="check_test()" /> Checkbox
<br />
<input type="radio" name="rb" id="r1" onclick="check_test()" /> Button 1
<input type="radio" name="rb" id="r2" onclick="check_test()" /> Button 2
<p>
Checkbox checked: <span id="cinfo">no</span>
<br />
Button1 checked: <span id="r1info">no</span>
<br />
Button2 checked: <span id="r2info">no</span>
</p>
</body>
</html>
<html>
<head>
<title>Hoagie with Extras</title>
<script type="text/javascript">
<!--
function test()
{
var output = document.getElementById( "output" )
var m = document.getElementById( "m" )
var c = document.getElementById( "c" )
var p = document.getElementById( "p" )
var t = document.getElementById( "t" )
var cost = 4.95; // base cost
if (m.checked) cost = cost + .71
if (c.checked) cost = cost + .50
if (p.checked) cost = cost + .22
if (t.checked) cost = cost + .16
output.innerHTML = "Please pay the server: $" + cost
}
// -->
</script>
</head>
<body>
Order a hoagie:<br />
<input type="checkbox" id="m" /> extra meat ?<br />
<input type="checkbox" id="c" /> extra cheese ?<br />
<input type="checkbox" id="p" /> extra pickles ?<br />
<input type="checkbox" id="t" /> extra tomatoes ?<br />
<button onclick="test()">Calculate Cost</button>
<p id="output"></p>
</body>
</html>
<html>
<head>
<title>Tax Calculator</title>
<script type="text/javascript">
<!--
function compute()
{
var sal = document.getElementById( "sal" )
var sgl = document.getElementById( "sgl" )
var mfj = document.getElementById( "mfj" )
var mfs = document.getElementById( "mfs" )
var tax = document.getElementById( "tax" )
var num = parseFloat(sal.value)
tax.innerHTML = "" // clear it initially
if (isNaN(num))
alert( "Invalid Input" )
else {
var result
if (sgl.checked)
result = num * .12
else if (mfs.checked)
result = num * .14
else if (mfj.checked)
result = num * .23
result = Math.round( 100 * result ) / 100
tax.innerHTML = result
}
}
// -->
</script>
</head>
<body>
Salary: <input type="text" id="sal" />
<p>
<input type="radio" name="status" id="sgl" checked /> Single
<br />
<input type="radio" name="status" id="mfs" /> Married filing separately
<br />
<input type="radio" name="status" id="mfj" /> Married filing jointly
</p>
<button onclick="compute()">Compute Tax</button>
<p>
Tax due: <span id="tax"></span>
</p>
</body>
</html>