javascript
javascript
An HTML form is used to collect user input. The user input is most often sent to a server for
processing.
The HTML <form> element is used to create an HTML form for user input:
<form>
.
form elements
.
</form>
The <form> element is a container for different types of input elements, such as: text fields,
checkboxes, radio buttons, submit buttons, etc.
Usually, the form data is sent to a file on the server when the user clicks on the submit
button.
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
The target attribute specifies where to display the response that is received after
submitting the form.
The method attribute specifies the HTTP method to be used when submitting the form data.
The form-data can be sent as URL variables (with method="get") or as HTTP post
transaction (with method="post").
GET:
POST:
Appends the form data inside the body of the HTTP request (the submitted form data
is not shown in the URL)
POST has no size limitations, and can be used to send large amounts of data.
Form submissions with POST cannot be bookmarked
The autocomplete attribute specifies whether a form should have autocomplete on or off.
An <input> element can be displayed in many ways, depending on the type attribute.
The input value attribute specifies an initial value for an input field:
The input readonly attribute specifies that an input field is read-only.A read-only input field cannot be
modified (however, a user can tab to it, highlight it, and copy the text from it).The value of a read-only
input field will be sent when submitting the form!
The input disabled attribute specifies that an input field should be disabled.A disabled input field is
unusable and un-clickable.The value of a disabled input field will not be sent when submitting the
form!
The input size attribute specifies the visible width, in characters, of an input field.The default value
for size is 20.
The input maxlength attribute specifies the maximum number of characters allowed in an input
field.The input min and max attributes specify the minimum and maximum values for an input
field.The min and max attributes work with the following input types: number, range, date, datetime-
local, month, time and week.
The input required attribute specifies that an input field must be filled out before submitting the form.
The input autofocus attribute specifies that an input field should automatically get focus when the page
loads.
The input autocomplete attribute specifies whether a form or an input field should have autocomplete
on or off.
The required attribute works with the following input types: text, search, url, tel, email, password, date
pickers, number, checkbox, radio, and file.
Text Fields
The <input type="text"> defines a single-line input field for text input.
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname">
</form>
The <label> element is useful for screen-reader users, because the screen-reader will read
out loud the label when the user focuses on the input element.
The <label> element also helps users who have difficulty clicking on very small regions
(such as radio buttons or checkboxes) - because when the user clicks the text within
the <label> element, it toggles the radio button/checkbox.
The for attribute of the <label> tag should be equal to the id attribute of
the <input> element to bind them together.
Radio Buttons
<form>
<input type="radio" id="html" name="fav_language" value="HTML">
<label for="html">HTML</label><br>
<input type="radio" id="css" name="fav_language" value="CSS">
<label for="css">CSS</label><br>
</form>
Checkboxes
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
</form>
The form-handler is typically a file on the server with a script for processing input data.
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
If the name attribute is omitted, the value of the input field will not be sent at all.
Use the multiple attribute to allow the user to select more than one value:
With the HTML DOM, JavaScript can access and change all the elements of an HTML
document.
The HTML DOM (Document Object Model)
When a web page is loaded, the browser creates a Document Object Model of the
page.The HTML DOM model is constructed as a tree of Objects:
With the object model, JavaScript gets all the power it needs to create dynamic HTML:
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that
allows programs and scripts to dynamically access and update the content, structure, and
style of a document."
The HTML DOM is a standard object model and programming interface for HTML. It
defines:
In other words: The HTML DOM is a standard for how to get, change, add, or delete
HTML elements.
The HTML DOM can be accessed with JavaScript (and with other programming languages).In
the DOM, all HTML elements are defined as objects.The programming interface is the
properties and methods of each object.A property is a value that you can get or set (like
changing the content of an HTML element).A method is an action you can do (like add or
deleting an HTML element).
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
The most common way to access an HTML element is to use the id of the element.In the
example above the getElementById method used id="demo" to find the element.
If you want to access any element in an HTML page, you always start with accessing the
document object.
Reacting to Events
A JavaScript can be executed when an event occurs, like when a user clicks on an HTML
element.
To execute code when a user clicks on an element, add JavaScript code to an HTML event
attribute:
onclick=JavaScript
In this example, the content of the <h1> element is changed when a user clicks on it:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
<html>
<body>
<script>
function changeText(id) {
id.innerHTML = "Ooops!";
}
</script>
</body>
</html>
The HTML DOM allows you to assign events to HTML elements using JavaScript:
<script>
document.getElementById("myBtn").onclick = displayDate;
</script>
The onload and onunload events are triggered when the user enters or leaves the page.
The onload event can be used to check the visitor's browser type and browser version, and
load the proper version of the web page based on the information.
The onload and onunload events can be used to deal with cookies.
<body onload="checkCookies()">
The onchange event is often used in combination with validation of input fields.
Below is an example of how to use the onchange. The upperCase() function will be called
when a user changes the content of an input field.
The onmouseover and onmouseout events can be used to trigger a function when the user
mouses over, or out of, an HTML element:
Mouse Over Me
The onmousedown, onmouseup, and onclick events are all parts of a mouse-click. First when
a mouse-button is clicked, the onmousedown event is triggered, then, when the mouse-
button is released, the onmouseup event is triggered, finally, when the mouse-click is
completed, the onclick event is triggered.
Input Events
onblur - When a user leaves an input field onchange - When a user changes the content of
an input field onchange - When a user selects a dropdown value onfocus - When an input
field gets focus onselect - When input text is selected onsubmit - When a user clicks the
submit button onreset - When a user clicks the reset button onkeydown - When a user is
pressing/holding down a key onkeypress - When a user is pressing/holding down a
keyonkeyup - When the user releases a key onkeyup - When the user releases a key
onkeydown vs onkeyup - Both
<!DOCTYPE html>
<html>
<body>
<script>
function upperCase() {
const x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(x) {
x.style.background = "yellow";
}
</script>
</head>
<body>
<p>When the input field gets focus, a function is triggered which changes the background-color.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</head>
<body>
<p>A function is triggered when the user is pressing a key in the input field.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</head>
<body>
<p id="demo"></p>
</body>
</html>