-
The best known form on the web is probably the search box that sits right in the middle of Google's homepage.
-
There are several types of form controls that you can use to collect information from visitors to your site.
-
-
Used for a single line of text such as email addresses and names.
-
Like a single line text box but it masks the characters entered.
-
For longer areas of text, such as messages and comments.
-
-
-
For use when a user must select one of a number of options.
-
When a user can select and unselect one or more options.
-
When a user must pick one of a number of options from a list.
-
-
-
To submit data from your form to another web page.
-
Similar to submit buttons but they allow you to use an image.
-
Allows users to upload files (e.g. images) to a website.
-
-
-
-
A user fills in a form and then presses a button to submit the information to the server.
-
The name of each formcontrol is sent to the server along with thevalue the user enters or selects.
-
The server processes the information using a programming language such as
PHP,C#,VB.net, orJava. It may also store the information in a database. -
The server creates a new page to send back to the browser based on the information received.
-
-
-
<form>Form controls live inside a
<form>element. This element should always carry the action attribute and will usually have a method and id attribute too. -
actionEvery
<form>element requires an action attribute. Its value is the URL for the page on the server that will receive the information in the form when it is submitted. -
methodForms can be sent using one of two methods:
getorpost. -
-
<input>The
<input>element is used to create several different form controls. The value of the type attribute determines what kind of input they will be creating. -
type="text"When the type attribute has a value of text, it creates a singleline text input.
-
nameWhen users enter information into a form, the server needs to know which form control each piece of data was entered into. (For example, in a login form, the server needs to know what has been entered as the username and what has been given as the password.) Therefore, each form control requires a name attribute. The value of this attribute identifies the form control and is sent along with the information they enter to the server.
-
maxlengthYou can use the maxlength attribute to limit the number of characters a user may enter into the text field. Its value is the number of characters they may enter. For example, if you were asking for a year, the maxlength attribute could have a value of 4.
-
-
-
<input type="password">When the type attribute has a value of password it creates a text box that acts just like a single-line text input, except the characters are blocked out. They are hidden in this way so that if someone is looking over the user's shoulder, they cannot see sensitive data such as passwords.
-
nameThe name attribute indicates the name of the password input, which is sent to the server with the password the user enters.
-
size, maxlengthIt can also carry the size and maxlength attributes like the the single-line text input.
-
-
-
<textarea>The
<textarea>element is used to create a mutli-line text input. Unlike other input elements this is not an empty element. It should therefore have an opening and a closing tag.Any text that appears between the opening
<textarea>and closing</textarea>tags will appear in the text box when the page loads.
-
-
-
<input type="radio">Radio buttons allow users to pick just one of a number of options.
-
nameThe name attribute is sent to the server with the value of the option the user selects. When a question provides users with options for answers in the form of radio buttons, the value of the name attribute should be the same for all of the radio buttons used to answer that question.
-
valueThe value attribute indicates the value that is sent to the server for the selected option. The value of each of the buttons in a group should be different (so that the server knows which option the user has selected).
-
checkedThe checked attribute can be used to indicate which value (if any) should be selected when the page loads. The value of this attribute is checked. Only one radio button in a group should use this attribute.
-
-
-
-
list-style-typeThe list-style-type property allows you to control the shape or style of a bullet point (also known as a marker). It can be used on rules that apply to the
<ol>,<ul>, and<li>elements. -
For an unordered list you can use the following values:
- none
- disc
- circle
- square
-
For an ordered (numbered) list you can use the following values:
-
-
You have already met several properties that are commonly used with tables. Here we will put them together in a single example using the following:
-
widthto set the width of the table
-
paddingto set the space between the border of each table cell and its content
-
text-transformto convert the content of the table headers to uppercase
-
letter-spacing, font-sizeto add additional styling to the content of the table headers
-
border-top, border-bottomto set borders above and below the table headers
-
text-alignto align the writing to the left of some table cells and to the right of the others
-
background-colorto change the background color of the alternating table rows
-
:hoverto highlight a table row when a user's mouse goes over it
-
-
-
empty-cellsIf you have empty cells in your table, then you can use the
empty-cellsproperty to specify whether or not their borders should be shown. -
showThis shows the borders of any empty cells.
-
hideThis hides the borders of any empty cells.
-
inheritIf you have one table nested inside another, the inherit value instructs the table cells to obey the rules of the containing table.
-
-
The
border-spacingproperty allows you to control the distance between adjacent cells. By default, browsers often leave a small gap between each table cell, so if you want to increase or decrease this space then theborder-spacingproperty allows you to control the gap.border-collapse property. Possible values are:
-
collapseBorders are collapsed into a single border where possible. (border-spacing will be ignored and cells pushed together, and empty-cells properties will be ignored.)
-
separateBorders are detached from each other. (border-spacing and empty-cells will be obeyed.)
-
-
-
-
font-sizesets the size of the text entered by the user.
-
color, background-colorcolorsets the text color, andbackground-colorsets the background color of the input. -
border, border-radiusborderadds a border around the edge of the input box, andborder-radiuscan be used to create rounded corners (for browsers that support this property). -
:focus, :hoverThe
:focuspseudo-class is used to change the background color of the text input when it is being used, and the:hoverpsuedo-class applies the same styles when the user hovers over them. -
background-imageadds a background image to the box. Because there is a different image for each input, we are using an attribute selector looking for the value of the id attribute on each input.
-
-
-
colorcoloris used to change the color of the text on the button. -
text-shadowtext-shadow can give a 3D look to the text in browsers that support this property.
-
border-bottomhas been used to make the bottom border of the button slightly thicker, which gives it a more 3D feel.
-
background-colorcan make the submit button stand out from other items around it. (Creating a consistent style for all buttons helps users understand how they should interact with the site.) A gradient background has been added for browsers that support gradients.
-
-
-
When the user interacts with the HTML on a web page, there are three steps involved in getting it to trigger some JavaScript code. Together these steps are known as event handling.
-
Select t he elementnode(s) you want the script to respond to.
For example, if you want to trigger a function when a user clicks on a specific link, you need to get the DOM node for that link element. You do this using a DOM query
-
Indicate which event on the selected node(s) will trigger the response.
Programmers call this binding a event to a DOM node.
-
State the code you want to run when the event occurs.
-
-
-
HTML EVENT HANDLERS
This is bad practice, but you need to be aware of it because you may see it in older code.
Early versions of HTML included a set of attributes that could respond to events on the element they were added to. The attribute names matched the event names. Their values called the function that was to run when that event occurred.
i.g <a onclick="hide()"> -
TRADITIONAL DOM EVENT HANDLERS
DOM event handlers were introduced in the original specification for the DOM. They are considered better than HTML event handlers because they let you separate the JavaScript from the HTML.
-
Event listeners were introduced in an update to the DOM specification (DOM level 2, released in the year 2000). They are now the favored way of handling events.
The syntax is quite different and, unlike traditional event handlers, these newer event listeners allow one event to trigger multiple functions. As a result, there are less likely to be conflicts between different scripts that run on the same page.
-