HTML forms are certainly not well liked, and are very rarely considered fun. However, there are ways to make your forms look and function more concisely. Putting the labels inside your form fields can shorten up your forms, save space, and allow a little more creativity in your form design.
In a normal form, you may have your labels above or before your form fields.
Instead of taking up twice the amount of space by attaching a label, we can instead put a preset value inside the input in place of the label.
Now our label looks a lot cleaner on load. Unfortunately, since we only used a value, upon the user focusing on the field, the text inside will act as if it was a value typed in. This means the user will have to delete the value before entering their own. This certainly doesn't act like a label, and is a pretty poor user experience. How about we use some JavaScript to tell the form to empty when the user focuses?
Much nicer! Let's break down what we added.
onfocus
will run whenever the input is focused on. This means that whenever this input is focused, this will check to see if the string "Last Name" is the current value of the input. If it is (which it is by default), focusing will erase the string. We want to be specific here, because if there is a different string present, we can assume that the user has entered their information.
This is certainly a better experience for the user than what we started with, however there is one thing that could still cause problems with this input. If a user clicks on the field, making the default value disappear, and then leaves and focuses on a different field, our input will stay blank. Let's use an extreme use case to illustrate why this is a big problem:
Your user reads books by following along with their finger, so naturally when they read text on a computer they follow along with their cursor. The user visits your form with the above code and decides to read the entire form before filling it out. In reading through, he clicks on each of the inputs. Since the value doesn't come back once it is gone, your user will end up with a lot of blank input fields and no way to tell what information to enter in what box.
Try it yourself:
An answer to this is to use the opposite of onfocus
, which is onblur
. This runs whenever the input loses focus. Here, we'll run the opposite conditional as we did before, ensuring that if the form field is empty and loses focus, our value "label" will return to inform the user. This also lets the user simply click outside the field if they forget the label.
Now if we add this to the same list of inputs as the previous example, clicking through all of them will not end up in any confusion, as the values will return when unfocused.
Now you have relatively smart form values that will act as labels, and will keep both your design team and your clients happy. If you'd like a more robust system than can be had from one line, check out this more elaborate example from Stefano J. Attardi.