• Stars
    star
    223
  • Rank 178,458 (Top 4 %)
  • Language
    JavaScript
  • License
    GNU General Publi...
  • Created almost 13 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Small and robust polyfill for the HTML5 placeholder attribut.

HTML5 placeholder Polyfill

Lightweight and very robust little jQuery plugin that generates the look and feel of the HTML5 placeholder attribute for browsers without native support. It also adds an extra title in case the placeholder text is too long to be displayed. The polyfill comes with an option to define if the placeholder text should be read to screenreaders or not (on by default). Since version 1.9 the default option is to make it behave like Chrome or mobile Safari (hide placeholder when the users enters content rather than when the fields receives focus).

Demos:

To see the actual work of this polyfill use an old browser like Firefox 3.6!

Dependencies:

Required:

  • jQuery (tested with 1.6.2 but might as well work with older versions)

Optional but recommended:

  • Modernizr (tested with 2.0.6) OR yepnope.js

    • yepnope.js is included with Modernizr by default. Just make sure the setting in the Extras-Block: "Modernizr.load (yepnope.js)" is checked on the Modernizr Download Page
  • fontresize (excellent even though terribly unmaintained event plugin that fires when a user changes the font size of their browser (that usually breaks the other placeholder polyfills))

Optional

Required if hiding the placeholder when user types instead of onfocus (like Chrome or mobile Safari) {hideOnFocus : false}

Highly optional

only needed if you want users to be able to resize textareas:

  • jquery-resize if included a repositioning is triggered when a user resizes a textarea. If not I disable the resizing of textareas to avoid rendering problems

Browser Support

The placeholder attribute has decent support across current Browsers. This script adds support for the older generations including:

  • Internet Explorer < 10
  • Firefox < 4.0
  • Safari < 4.0
  • iOS Safari < 4.0
  • Android Browser < 2.0

For more details on native support see the browser suppport table at caniuse.com.

Installation

You can install HTML5 placeholder Polyfill by using Bower.

bower install html5-placeholder-polyfill

USAGE:

Simply include the Javascript and CSS. The Polyfill will only run when needed.

<head>
	<link rel="stylesheet" href="placeholder_polyfill.min.css">
	<script src="placeholder_polyfill.jquery.min.combo.js" charset="utf-8"></script>
</head>

Please bear in mind that every input needs a linked label in order for the plugin to work.

Using Modernizr, modern browser don't even have to load the polyfill at all.

<script>
Modernizr.load({
    test: Modernizr.input.placeholder,
    nope: [
			'placeholder_polyfill.min.css',
			'placeholder_polyfill.jquery.min.combo.js'
          ]
});
</script>

Using yepnope.js (used as load() in Modernizr), the same as with Modernizr, but with manual feature detection.

<script>
yepnope({
    test: ('placeholder' in $('<input>')[0]),
    nope: [
            'placeholder_polyfill.min.css',
            'placeholder_polyfill.jquery.min.combo.js'
          ]
});
</script>

Configuring the behavior (optional)

<head>
	<link rel="stylesheet" href="placeholder_polyfill.min.css">
	<script>
	placeHolderConfig = {
		// css class that is used to style the placeholder
        	className: 'placeholder',
        // expose the placeholder text to screenreaders or not
            visibleToScreenreaders : true,
        // css class is used to visually hide the placeholder
	        visibleToScreenreadersHideClass : 'placeholder-hide-except-screenreader',
	    // css class used to hide the placeholder for all
            visibleToNoneHideClass : 'placeholder-hide',
        // either hide the placeholder on focus or on type
            hideOnFocus : false,
        // remove this class from a label (to fix hidden labels)
            removeLabelClass : 'visuallyhidden',
        // replace the label above with this class
            hiddenOverrideClass : 'visuallyhidden-with-placeholder',
        // allow the replace of the removeLabelClass with hiddenOverrideClass or not
            forceHiddenOverride : true,
        // apply the polyfill even for browser with native support
            forceApply : false,
        // init automatically or not
        	autoInit : true
	}
	</script>
	<script src="placeholder_polyfill.jquery.min.combo.js" charset="utf-8"></script>
</head>

Customizing the styling (optional)

<head>
	<style>
		/* Example how to style the placeholder itself */
		::-webkit-input-placeholder, /* Chrome/Opera/Safari */
		::-moz-placeholder, /* Firefox 19+ */
		:-ms-input-placeholder, /* IE 10+ */
		:-moz-placeholder, /* Firefox 18- */
		::placeholder, /* standard */
		label .placeholder  /* this polyfill */
		{
		    color:red; /* your awesome style for the the placeholder text */
		}

		/* Example how to style the :placeholder-shown pseudo class so it works with this polyfill: */
		input:placeholder-shown,
		input.placeholder-shown  /* just duplicate the selector and change the :peudo selector to a .class selector */
		{
		    border: 2px solid red /* your awesome style for the input element when the placeholder is visible */
		}
	</style>
</head>

VERSION HISTORY

See the complete Version History here

More Repositories

1

Accessible-Tabs

jQuery Plugin that generates a Tab Navigation from Markup that makes sense without Javascript. The generated Tabs are the only ones out there that work for Screenreader users without support for WAI ARIA.
CSS
127
star
2

syncHeight

jQuery Plugin that automatically syncs the height properties of the elements that match the given selector.
HTML
85
star
3

CSS-Hacks

Filters to target all A-Grade Browsers individually in CSS
JavaScript
41
star
4

simpleCachedCurl

easy to use cURL wrapper with added file cache
PHP
25
star
5

Accessible-Carousel

jQuery Accessible Carousel Plugin
JavaScript
12
star
6

Accessible-Longdesc

making the Longdesc content available to users without assistive technologies
JavaScript
11
star
7

easychirp

easychirp.com - Web accessibility for the Twitter website application
HTML
11
star
8

LogFocus

LogFocus - handy Bookmarklet for Keyboard Accessibility testing
JavaScript
6
star
9

JsPopUp

Opening Browser-Windows the unobtrusive way
JavaScript
5
star
10

Misconceptions

Daily Myths — Visualize the “All-too-Common Astronomy/Solar System Misconceptions”
HTML
5
star
11

dropdownNav

jQuery Plugin that turns a regular form select element into a keyboard accessible navigation element
JavaScript
5
star
12

DebugLog

nicer Javascript console logging
JavaScript
5
star
13

skipnavHighlight

jQuery Plugin that nicely outlines around the targets of the anchors in the given selector.
HTML
4
star
14

JsFormCheck

Javascript Class for generic unobtrusive and accessible clientside Form Validation
JavaScript
4
star
15

resume

my current resume
JavaScript
3
star
16

Bubble-Breaker

A Javascript Clone of the classic Windows Mobile game
JavaScript
3
star
17

mixins

a selection of mixins for CSS Precompilers
Ruby
2
star
18

test

1
star
19

ginader.github.com

Repository to run my Projects on github
1
star