reading-notes

https://faroukibrahim-fii.github.io/reading-notes/

View the Project on GitHub FaroukIbrahim-FII/reading-notes

Forms and Events

Forms

Why Forms?

The best known form on the web is probably the search box that sits right in the middle of Google’s homepage.

Form Controls

There are several types of form controls that you can use to collect information from visitors to your site.

How Forms Work

  1. A user fills in a form and then presses a button to submit the information to the server.
  2. The name of each form control is sent to the server along with the value the user enters or selects.
  3. The server processes the information using a programming language such as PHP, C#, VB.net, or Java. It may also store the information in a database.
  4. The server creates a new page to send back to the browser based on the information received.

Form Structure

Text Input

Password Input

Text Area

<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.

Radio Button

Checkbox

Other Input Options

Lists, Tables and Forms

Lists

There are many ways to style the lists:

lists

Table Properties

Styling Forms

Nobody I know enjoys filling in forms, so if you can make yours look more attractive and easier to use, more people are likely to fill it in. Also, when you come to look at a form in a few different browsers (as shown on the right), you will see that each browser displays them differently.

Styling Text Inputs

This example demonstrates the CSS properties commonly used with text inputs, most of which you have already met.

font-size sets the size of the text entered by the user.

color sets the text color, and background-color sets the background color of the input.

border adds a border around the edge of the input box, and border-radius can be used to create rounded corners (for browsers that support this property).

Styling Submit Buttons

Here are some properties that can be used to style submit buttons. This example builds on the one in the previous page, and the submit button inherits the styles set for the element on the last page.

color is used to change the color of the text on the button. text-shadow can give a 3D look to the text in browsers that support this property.

border-bottom has been used to make the bottom border of the button slightly thicker, which gives it a more 3D feel.

Styling Fieldsets & Legends

Fieldsets are particularly helpful in determining the edges of a form. In a long form they can help group together related information within it.

Aligning Form Controls: Problem

Labels for form elements are often different lengths, which means that the form controls will not appear in a straight line. This is demonstrated in the example on the right (without CSS applied to the form controls).

Aligning Form Controls: Solution

Each row of the form has a title telling users what they need to enter. For the text inputs, the title is in the <label> element. For the radio buttons, the title is in a <span> element. Both have a class attribute with a value of title.

Cursor Styles

The cursor property allows you to control the type of mouse cursor that should be displayed to users.

Here are the most commonly used values for this property:

Events

Here is a selection of the events that occur in the browser while you are browsing the web. Any of these events can be used to trigger a function in your JavaScript code.

event

How Events Trigger JavaScript Code

  1. Select t he element node(s) you want the script to respond to.
  2. Indicate which event on the selected node(s) will trigger the response.
  3. State the code you want to run when the event occurs.

THREE WAYS TO BIND AN EVENT TO AN ELEMENT

Event handlers let you indicate which event you are waiting for on any particular element. There are three types of event handlers.

TRADITIONAL DOM EVENT HANDLERS

All modern browsers understand this way of creating an event handler, but you can only attach one function to each event handler.

event

SUPPORTING OLDER VERSIONS OF IE

IES-8 had a different event model and did not support addEventL i stener() but you can provide fallback code to make event listeners work with older versions of IE.

Event Flow

HRML elements nest inside other elements. If you hover or click on a link, you will also be hovering or clicking on its parent elements.

Why Flow Matters

The flow of events only really matters when your code has event handlers on an elemetn and one of its ancestor or descendant elements.

THE EVENT OBJECT

When an event occurs, the event object tells you information about the event, and the element it happened upon.

WHICH ELEMENT DID AN EVENT OCCUR ON?

When calling a function, the event object’s target property is the best way to determine which element the event occurred on. But you may see the approach below used; it relies on the this keyword.

THE this KEYWORD The this keyword refers to the owner of a function. On the right, this refers to the element that the event is on. This works when no parameters are being passed to the function.

DIFFERENT TYPES OF EVENTS

The DOM events specification is managed by the W3C (who also look after other specifications including HTML, CSS, and XML). Most of the events you will meet in this chapter are part of this DOM events specification.

The HTMLS specification (that is still being developed) details events that browsers are expected to support that are specifically used with HTML.

Browser manufacturers also implement some events as part of their Browser Object Model (or BOM). Typically these are events not (yet) covered by W3C specifications.

USER INTERFACE EVENTS

User interface CUI) events occur as a result of interaction with the browser window rather than the HTML page contained within it, e.g., a page having loaded or the browser window being resized.

Where Events Occur

The event object can tell you where the cursor was positioned when an event was triggered.