Placeholder

current release: v1.1.0

How it works - $(selector).placeholder({options});

jQuery Placeholder plugin emulates the HTML5 placeholder attribute where not supported. The effect is similar to pre-populated text inside input fields that clear when in focus. See W3C Placeholder Attribute

Call $(selector).placeholder(options) on an input element with an attribute placeholder.

$('input#search').placeholder();
<form>
  <input id="search" name="q" placeholder="Search in books">
  <button type="submit">Search</button>
</form>
Placeholder Properties
Arguments Type Description
onFocus string Class to apply when input is focused. Default is focus.
onBlur string Class to apply when input is blurred. Default is false.
onChange string Class to apply if input value has changed after blurred. Default is false.
placeholderSupport boolean By default, the plugin will check for the browser's placeholder attribute support. Set to false or true to bypass.

Setup

Include Dependencies

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery/jquery.placeholder.js"></script>

Call $(selector).placeholder(options) on elements when DOM is ready

<script>
$(function(){
  $('input#search').placeholder();
});
</script>

Examples

Sample HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Placeholder Example</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery/jquery.placeholder.js"></script>

<script>
$(function(){
$('input[placeholder]').placeholder({onBlur: "blurred", onChange: "changed"});
});
</script>
<style>
input {border:2px solid black; color: black;}
.focused {border:2px solid red; color:red}
.blurred {border:2px solid green; color:green}
.changed {border:2px solid blue; color:blue}
</style>

</head>
<body>
<form>
<input id="search" name="q" placeholder="Search in books">
<button type="submit">Search</button>
</form>

</body>
</html>

Example with Options

JavaScript

$('input#search').placeholder({onBlur: "blurred", onChange: "changed"});

Example with Global "Placeholder" Binding

JavaScript

<script>
   $('input#search').placeholder({onBlur: "blurred", onChange: "changed"});
</script>
<form>
<input name="q" placeholder="Search in books NV"><br />
<input name="q" placeholder="Search in books BV" value=" "><br />
<input name="q" placeholder="Search in books" value="Harry Potter"><br />
<input name="q">
<button type="submit">Search</button>
</form>



Try these test on the above example.

  1. Change values above and hit refresh. The normal behavior should keep the changes until the user does a cache-free refresh or update.
  2. Copy fields with placeholder while keeping event behaviors. Try it

Latest Source

Changelog

Documentation was last modified on 25 October 2011 at 11:51 PM
01.19.2011 Version 1.1.0
Updated to use placeholder attribute in all browsers. Modified when to change values and update classes to mimic standard placeholder behavior.
07.07.2009 Version 1.0.0
Initial release.