Module 5 Web Design
Module 5 Web Design
Module 5 Web Design
Buttons
There are different types of buttons, each with its own syntax. Here’s is one of its syntax:
<input type="button" value="button-label" onclick="click-event-handler">
Note that the code is a void element that uses the input tag, the input tag implements elements that handle user input. The
input element is used for different types of user input, and its type attribute specifies which type of user input. The type
attribute specifies the type of control that’s being implemented, where a control is a user input entity such as a button, text
control, or checkbox. In the Hello web page source code, note that the type attribute gets the value button, which tells the
browser to display a button. If you don’t provide a type attribute, the browser will display a text control, because that’s the
default type of control for the input element. A text control is a box that a user can enter text into, and this is what a text
control looks like:
The input element’s value attribute specifies the button’s label. The input element’s onclick attribute specifies the
JavaScript instructions that the JavaScript engine executes when the user clicks the button. A JavaScript engine is the part
of the browser software that runs a web page’s JavaScript. Clicking the button is considered to be an event, so the onclick
attribute’s JavaScript code is known as an event handler. In the Hello web page source code, note that the onclick
attribute’s value is simply displayHello();. That calls the displayHello function, which is defined in the web page’s script
block.
Functions
A function in JavaScript is similar to a mathematical function. A mathematical function receives arguments, performs a
calculation, and returns an answer. Here’s the syntax for calling a function:
function function-name(zero-or-more-parameters-separated-by-commas) {
statement-1;
statement-2;
…
last-statement;
}
Variables
You can think of a variable as a box that holds a value. Before you use a variable in JavaScript code, you should use var
to declare the variable in a declaration statement. For example:
var name;
var careerGoals;
Words that are part of the JavaScript language are known as keywords and are “reserved” for the JavaScript language, and
they are known as reserved words.
The variable’s type is determined dynamically by the type of the value that’s assigned into the variable. For example:
JavaScript is known as a loosely typed language, or a dynamically typed language, which means that you do not declare a
variable’s data type explicitly, and you can assign different types of values into a variable at different times during the
execution of a program.
Identifiers
An identifier is the technical term for a program component’s name—the name of a function, the name of a variable, and
the names of other program components. Identifiers must consist entirely of letters, digits, dollar signs ($), and/or
underscore (_) characters. The first character must not be a digit.
function displayHello() {
var msg;
msg = document.getElementById("message");
msg.outerHTML = "<h1>Hello, world!</h1>";
}
The first assignment statement, the document.getElementById("message") thing gets assigned into the msg variable. In
the second assignment statement, the "<h1>Hello, world!</h1>" thing gets assigned into the msg.outerHTML variable.
To understand the syntax requires an understanding of objects. An object is a software entity that represents something
tangible. All of the elements in a web page are represented as objects. When a browser loads the Hello web page, the
browser software generates objects for the head element, the body element, the h3 element, and so on. There’s also an
object associated with the entire web page, and that object’s name is document. Each object has a set of related properties,
plus a set of behaviors. A property is an attribute of an object. A behavior is a task that the object can perform. The
document object contains properties like the web page’s type.
An object is not only a set of properties, but also a set of behaviors. One of the document object’s behaviors is its ability
to retrieve an element using the element’s id value. In JavaScript (and many other programming languages, as well), an
object’s behaviors are referred to as methods. To retrieve an element, the document object uses its getElemementById
method. To call an object’s method, you specify the object name, a dot, the method name, and then parentheses around
any arguments you want to pass to the method. For example:
document.getElementById("message")
The term dynamic HTML refers to updating the web page’s content by manipulating the DOM’s nodes. The DOM
provides different ways to access the nodes in the node tree. Here are three common techniques:
1. You can retrieve the node tree’s root by using document (for the document object) in your code and then use the root
object as a starting point in traversing down the tree.
2. You can retrieve the node that the user just interacted with (e.g. a button that was clicked) and use that node object as a
starting point in traversing up or down the tree.
3. You can retrieve a particular element node by calling the document object’s getElementById method with the element’s
id value as an argument.
form Element
Template for the form element’s syntax:
<form>
label
text-box, list-box, check-box, etc.
label
text-box, list-box, check-box, etc.
...
submit-button
</form>
Controls
Text Control
<input type="text" id="text-box-identifier" placeholder="user-entry-description"
size="box-width" maxlength="maximum-typed-characters">
Example:
<input type="text" id="ssn" placeholder="#########" size="9" maxlength="9">
Attributes
The id attribute’s value serves as an identifier for the text control, so it can be accessed with JavaScript.
The placeholder attribute provides a word or a short description that helps the user to know what to enter into the text
control.
The size attribute specifies the text control’s width, where the width value is an integer that approximates the number of
average-size characters that can fit in the box.
The maxlength attribute specifies the maximum number of characters that can be entered in the box.
The value attribute specifies an initial value for the text control.
Autofocus attribute specifies that after the page has loaded, the browser engine positions the cursor in the text control.
The disabled attribute specifies that the text control cannot receive the focus, and, therefore, the user cannot copy or edit
the text control’s value.
The readonly attribute specifies that the user can highlight the control’s value and copy it, but the user cannot edit it.
Event-Handler Attributes
<noscript>
<p>
This web page uses JavaScript. For proper results,
you must use a web browser with JavaScript enabled.
</p>
</noscript