Skip to content
Jabran Rafique

Form field placeholder fallback support

1 minute read

View source

Placeholders on form fields help with accessibility and save you space on labels. Older browsers don't support them, though, and older versions of Internet Explorer were the usual problem. I wrote this small jQuery plugin to fill in placeholders on browsers that lack them.

Add jQuery and the plugin to the head of your page.

<script src="path/to/jQuery.js"></script>
<script src="path/to/jquery.placeholderfallback.js"></script>

Then call placeholderfallback() on the fields that need it. Any normal selector works: an id, a class or the element itself.

$('#firstNameField').placeholderfallback();
$('.first-name-field').placeholderfallback();
$('input[type=text]').placeholderfallback();

The last one covers every input with a text type. To target several fields at once, pass multiple ids or classes.

$('#firstNameField, #lastNameField, #emailField').placeholderfallback();

A fuller example:

<form>
  <input type="text" id="firstNameField" placeholder="First Name" />
  <input type="text" id="lastNameField" placeholder="Last Name" />
  <input type="email" id="emailField" placeholder="Email address" />
</form>

<!--[if lt IE 10]>
  <script>
    (function () {
      $('#firstNameField, #lastNameField, #emailField').placeholderfallback();
    })();
  </script>
<![endif]-->

The plugin call sits inside Internet Explorer conditional comments, so it only runs on the browsers that need it.