Obtaining Form Input Using JavaScript
Obtaining input from a form with input elements or individual input elements is relatively straightforward. This is an important step to perform as it is required for input validation. As I mentioned in section 1, the HTML5 input element validation is far from perfect. All elements are not widely supported, and the validation provided is not always consistent or sufficient. So, processing input with JavaScript before submission is often necessary for sufficient accuracy. This helps to avoid the costly scenario where invalid input is sent to the server.
The "value" property
In JavaScript, all HTMLInputElements representing HTML input elements like <input> and <select> have a "value" property. This
allows us to get the value which is currently entered. The example below uses the getElementById() function to get the HTMLInputElement
representing the element in the form with an id attribute of "first-name". The "value" attribute can then be accessed and output in an alert
dialog box. Note that the value property is writable (it is not read-only). This means that it can also be used to change the actual value of
the DOM input element.
<form>
<input type = "text" id = "first-name" name = "first-name"
placeholder = "Eg. John"/>
<input type = "text" id = "last-name" name = "last-name"
placeholder = "Eg. Smith"/>
<input type = "email" id = "email" name = "email"
placeholder = "someone@example.com"/>
</form>
The relatively simple HTML form (above).
var firstNameElement = document.getElementById("first-name"); //Get the element with id "first-name".
alert(firstName.value); //Output current value in a dialog box.
firstName.value = "James"; //Change actual value.
alert(firstName.value); //Output will now be "James".
Adding event listeners
In most cases, we want to obtain an input value after the user has modified it. This can be done by using an event listener. If you are not familiar with event listeners already, the idea here is to monitor one or more DOM elements for a specific event. An event signals that something has occurred in the HTML document. This can be caused by a user action, such as clicking a button. Events can also be caused by the browser, such as completing the action of loading the page. An event listener is used to fire a function, known as a "callback", in response to an event. An event listener must be added to an event target, which receives the Event object representing the event. The most basic approach is to add the event listener directly to the element on which the event will occur.
Looking specifically at input elements, there are several events we could potentially listen for. These include:
- "change"
- Occurs when user changes an input value and the input element loses focus (Eg. user selects some other input element)
- "input"
- Similar to "change" event, but occurs the moment input value changes (Eg. each time user enters another character)
- "blur"
- Occurs when an input element loses focus, such as when user clicks elsewhere or presses "tab" key to go to different input field.
- "focus"
- When the input element gains focus. This is typically when user clicks on it to begin entering input, or when a "tab" press selects this element.
The following example shows how we can listen for the "change" event on the first name element (element with id "first-name").
This is accomplished by using the addEventListener() function on the element. This function takes 3 arguments.
The first argument is the name of the event we want to listen for, in this case "change". The
second argument is a reference to the callback function which is fired when the event occurs. This can be the name of a function
we have defined elsewhere, or an anonymous function defined directly within the argument, which is the approach used here.
This function simply outputs the current value. However, this function could be used to validate the input.
For example, we could check if the input is blank and whether it matches the pattern for a name. It is important to note here
that the value of "this" within the callback function is the element which has the event listener. So in this case, "this" refers to
our first name element. Finally, the third argument is optional and can often be ignored. However, there are cases where this argument
is important. See this guide if you want to learn about the third argument
var firstNameElement = document.getElementById("first-name"); //Get the element with id "first-name".
firstNameElement.addEventListener("change", function() {
var currentValue = this.value; //Get the value of the element which has the event listener.
alert(currentValue); //Output value.
});