Coolest Guides on the Planet

coolest guides on the planet

Coolest Guides On The Planet

  • Home
  • macOS
  • WebDev
  • All Posts
  • Contact

Add a Label into a Text Input Field in Form using HTML5 or Javascript

November 18, 2013 2 Comments

If you haven’t got a lot of room in your HTML form to put a label above or next to a text input field – you can put the label inside the input field instead!, this can be done in using Javascript or HTML 5 Form Attributes

Using Javascript Attributes; onblur and onfocus

Using a couple of dynamic Javascript attributes ‘onblur‘ and ‘onfocus, this changes the text dynamically based on the focus of the cursor in the text input fields from a default value to a newly entered value.

See the form below:

Tab through the fields and see the text disappear when the focus is on that field and then the original text re-appear if no text has been added in the field. The form has one field that has to be filled in correctly which is the email field.

To set up the form like this you need to add a couple of extra Javascript attributes in each required <input> field.

So for example in the first field the ‘first name’ field the input tag code would be this:

<input id="firstname" class="" name="firstname" type="text"  value="First Name" onfocus="if (this.value == 'First Name') {this.value = '';}" onblur="if (this.value == '') " />

The key tags being ‘onfocus‘ and ‘onblur‘.

The code will also work without the regular HTML attribute ‘value‘ tag but this will mean that the initial load of the page will not display the default value, so to include a loaded default value include that value attribute in the code.

Using HTML 5 Form Attributes; placeholder and required


Here the input field requires 2 HTML 5 form attributes, required and placeholder=”placeholder text”. The required attribute tells the browser a value is required and the placeholder does the same job as the onblur/onfocus attributes – just add the placeholder text in between the double quotes

The value attribute is not required as it will clash with the placeholder attribute.

So the equivalent input code for the first name field would be…

<input id="firstname" class="" name="firstname" type="text" placeholder="First Name" required />

If you click Download on the 2nd form without filling in values the browser will display a warning

You can also style the default input placeholder text by following this guide.

The HTML5 solution will work on modern browsers but only on IE10+, to get around earlier IE versions there is a fantastic polyfill javascript solution. I have also created a plugin for its use in WordPress

Cats: WebDev

Tags

3gs 10.6 apache backup baseband boot clean urls cpanel css curl custom database drupal el capitan git Google image instadmg ios iphone jailbreak keys lion mac macos mojave macos sierra menu mysql OSX panda php phpmyadmin private public redirect redsn0w remote rsa SEO shell ssh terminal unstoppables upgrade urls

Donate a Beer to the Coolest Guides

Get Beaver Builder Now!

Discuss

3gs 10.6 apache backup baseband boot clean urls cpanel css curl custom database drupal el capitan git Google image instadmg ios iphone jailbreak keys lion mac macos mojave macos sierra menu mysql OSX panda php phpmyadmin private public redirect redsn0w remote rsa SEO shell ssh terminal unstoppables upgrade urls
Get DesktopServer

Lynda

Lynda.com Online Training Videos

TreeHouse

smlinks

Learn WordPress
osx-modify-shell-path

How to Add to the Shell Path in macOS Big Sur and Catalina using Terminal

October 19, 2019

virtual-hosts osx 10.10 yosemite

Set up Virtual Hosts on macOS Catalina 10.15 in Apache

October 19, 2019

Installing Homebrew on macOS Catalina 10.15, Package Manager for Linux Apps

October 18, 2019

Where is the bash shell in macos Catalina?

October 12, 2019

Refine your search

  • All
  • Modules
  • Themes
  • Documentation
  • Forums & Issues
  • Groups

RSS ars technica

  • Matter update may finally take the tedium out of setting up your smart home
  • Cue: Apple will add AI search in mobile Safari, challenging Google
  • Apps like Kindle are already taking advantage of court-mandated iOS App Store changes
  • Spotify seizes the day after Apple is forced to allow external payments
  • Apple and Meta furious at EU over fines totaling €700 million

RSS mac surfer

  • Tot is new text editor for Mac, iPhone, and iPad focused on constraints and ease of use
  • TiPbITS: Google Drive Sorting Can Hide New Documents
  • How to take a screenshot on a MacBook Pro
  • How To Create Simple Animation With Mac Keynote
  • Last Week on My Mac: Virus pandemics

Donate

Copyright © 2025· Neil Gee - All Rights Reserved - Hosted by Runcloud

Copyright © 2025 · gee on Genesis Framework · WordPress · Log in