HTML Web Browser
HTML Web Browser
HTML Web Browser
Previous Page
Next Page
Web Forms 2.0 is an extension to the forms features found in HTML4.
Form elements and attributes in HTML5 provide a greater degree of
semantic mark-up than HTML4 and free us from a great deal of tedious
scripting and styling that was required in HTML4.
The <input> element in HTML4
HTML4 input elements use the type attribute to specify the data
type.HTML4 provides following types −
1 text
A free-form text field, nominally free of line breaks.
password
2
A free-form text field for sensitive information, nominally free
of line breaks.
3 checkbox
A set of zero or more values from a predefined list.
4 radio
An enumerated value.
5 submit
A free form of button initiates form submission.
6 file
An arbitrary file with a MIME type and optionally a file name.
image
7 A coordinate, relative to a particular image's size, with the extra
semantic that it must be the last value selected and initiates form
submission.
8 hidden
An arbitrary string that is not normally displayed to the user.
9 select
An enumerated value, much like the radio type.
10 textarea
A free-form text field, nominally with no line break restrictions.
button
11
A free form of button which can initiates any event related to
button.
datetime
1 A date and time (year, month, day, hour, minute, second,
fractions of a second) encoded according to ISO 8601 with the
time zone set to UTC.
2 datetime-local
A date and time (year, month, day, hour, minute, second,
fractions of a second) encoded according to ISO 8601, with no
time zone information.
date
3
A date (year, month, day) encoded according to ISO 8601.
month
4
A date consisting of a year and a month encoded according to
ISO 8601.
week
5
A date consisting of a year and a week number encoded
according to ISO 8601.
time
6
A time (hour, minute, seconds, fractional seconds) encoded
according to ISO 8601.
number
7
It accepts only numerical value. The step attribute specifies the
precision, defaulting to 1.
range
8
The range type is used for input fields that should contain a
value from a range of numbers.
9 email
It accepts only email value. This type is used for input fields
that should contain an e-mail address. If you try to submit a
simple text, it forces to enter only email address in
email@example.com format.
url
It accepts only URL value. This type is used for input fields that
10 should contain a URL address. If you try to submit a simple
text, it forces to enter only URL address either in
http://www.example.com format or in http://example.com
format.
<!DOCTYPE HTML>
<html>
<head>
<script type = "text/javascript">
function showResult() {
x = document.forms["myform"]["newinput"].value;
document.forms["myform"]["result"].value = x;
}
</script>
</head>
<body>
<!DOCTYPE HTML>
<html>
<body>
</body>
</html>
This will produce the following result −
The autofocus attribute
This is a simple one-step pattern, easily programmed in JavaScript at the
time of document load, automatically focus one particular form field.
HTML5 introduced a new attribute called autofocus which would be
used as follows −
<input type = "text" name = "search" autofocus/>
This attribute is supported by latest versions of Mozilla, Safari and
Chrome browsers only.
<!DOCTYPE HTML>
<html>
<body>
<form action = "/cgi-bin/html5.cgi" method = "get">
Enter email : <input type = "text" name = "newinput" autofocus/>
<p>Try to submit using Submit button</p>
<input type = "submit" value = "submit" />
</form>
</body>
</html>
The required attribute
Now you do not need to have JavaScript for client-side validations like
empty text box would never be submitted because HTML5 introduced a
new attribute called required which would be used as follows and
would insist to have a value −
<input type = "text" name = "search" required/>
This attribute is supported by latest versions of Mozilla, Safari and
Chrome browsers only.
Live Demo
<!DOCTYPE HTML>
<html>
<body>
<form action = "/cgi-bin/html5.cgi" method = "get">
Enter email : <input type = "text" name = "newinput" required/>
<p>Try to submit using Submit button</p>
<input type = "submit" value = "submit" />
</form>
</body>
</html>
It will produce the following result −
The global attributes are attributes that can be used with all HTML
elements.
Attribute Description
spellcheck Specifies whether the element is to have its spelling and grammar che
or not
style Specifies an inline CSS style for an element
<input type="search">
<input> elements of type search are text fields designed for the user to
enter search queries into. These are functionally identical to text inputs,
but may be styled differently by the user agent.
<input type="datetime-local">
<input> elements of type datetime-local create input controls that let the
user easily enter both a date and a time, including the year, month, and
day as well as the time in hours and minutes.
Definition and Usage
The resulting value includes the year, month, day, and time.
Tip: Always add the <label> tag for best accessibility practices!
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<label for="birthdaytime">Birthday (date and time):</label>
<input type="datetime-local" id="birthdaytime" name="birthdaytime">
<input type="submit">
</form>
</body>
</html>
HTML <input type="number">
Syntax
<input type="number">
Definition and Usage
Tip: Always add the <label> tag for best accessibility practices!
Syntax
<input type="color">
❮ HTML <input>
type
attribute- Creating an Editable Drop-Down
<input type="text" name="product" list="productName" />
<datalist id="productName">
<option value="Pen">Pen</option>
<option value="Pencil">Pencil</option>
<option value="Paper">Paper</option>
</datalist>