• Stars
    star
    107
  • Rank 323,587 (Top 7 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created over 16 years ago
  • Updated over 7 years ago

Reviews

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

Repository Details

jQuery plugin to populate form inputs with example text that disappears on focus. See also http://github.com/mudge/jquery_placeholder

jQuery Form Example Plugin 1.6.2 Build Status

This is a jQuery plugin to populate form inputs with example text that disappears on focus.

Basic usage revolves around the use of the example function like so:

$('input#name').example('Bob Smith');

This will then put the example text "Bob Smith" into the input with the ID "name". When a user then focuses on this input, the example text will disappear, allowing them to enter their data. If they then click elsewhere without entering any information, the example will re-appear.

By default, example text is given the class example, allowing you to style it with CSS like so:

.example { color: #666; }

If this class name conflicts with one you already use, you can override it with the className option like so:

$('input#name').example('Bob Smith', { className: 'hint' });

The above example would then be given the class hint instead of example.

If you plan to use the same options repeatedly, it is possible to override the plugin's defaults directly:

$.fn.example.defaults.className = 'notExample';

This will cause any uses of the plugin after this point to use the new defaults.

As well as supplying the example text via a string, a callback function can be used instead to determine the value at runtime:

$('input#name').example(function() {
   return $(this).attr('title'); 
});

The above example will set the example text to the title attribute of the input. The callback is executed within the context of the input field so, as in the example above, $(this) will return the input itself.

It is worth noting that the callback is evaluated every time the example text needs to be inserted and is not cached. This makes it possible to dynamically change the example text of a field after page load like so:

$('input#name').example(function() {
    var text = $(this).attr('title');
    $(this).attr('title', 'Not the original title anymore');
    return text;
});

The plugin also supports the jQuery Metadata plugin which allows you to specify metadata in elements themselves. You can specify the example text and any options like so:

<input id="m1" class="{example: 'An example', className: 'abc' }" />

Please note that metadata will be overridden by arguments, e.g.

<input id="m1" class="{example: 'An example'}" />
$('#m1').example('Another example');

The example will be set to "Another example" instead of "An example".

All events are namespaced with .example so they can be selectively unbound with unbind('.example'). The full list is as follows:

  • unload.example on the window;
  • submit.example on any affected forms;
  • focus.example on affected inputs;
  • change.example on affected inputs;
  • blur.example on affected inputs.

As of 1.6.0, if you wish to manually trigger clearing examples from a form (e.g. because you wish to override a form's submit event in some way) then you can trigger a special custom event, example:resetForm to do so:

$('#myform').trigger('example:resetForm');

Note that, due to event bubbling, triggering example:resetForm on a specific field will actual propagate up to its parent form and reset all examples.

/* This will actually bubble up to trigger on #myform. */
$('#myform input.name').trigger('example:resetForm');

Downloading

Download jQuery Example 1.6.2.

Or use Bower:

$ bower install jquery_example

Testing

As of 1.4, this plugin now comes with a QUnit test suite that you can find in the test/ directory and run by opening index.html in your browser.

Compatibility

This plugin has been tested with jQuery 1.6 to 1.7.0 and should work in all browsers supported by jQuery itself (it has been tested with Safari 5.1.1, Google Chrome 15.0.874.121, Mozilla Firefox 3.6, Mozilla Firefox 8, Opera 11.52 and Internet Explorer 6).

Author

This plugin was written by and is maintained by Paul Mucur aka "mudge". Please do not hesitate to contact me with comments and bug reports through GitHub: https://github.com/mudge/jquery_example/issues

You can view the latest source code (and fork the entire project if you wish) at http://github.com/mudge/jquery_example

Contributors

The code to support the Metadata plugin was contributed by DeLynn Berry (http://github.com/delynn).

Licensing

Copyright (c) Paul Mucur (http://mudge.name), 2007-2014.

Dual-licensed under the BSD (BSD-LICENSE.txt) and GPL (GPL-LICENSE.txt) Licenses.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Donations

Donations are accepted using Pledgie

More Repositories

1

re2

Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to backtracking regular expression engines like those used in PCRE, Perl, and Python".
Ruby
128
star
2

pacta

An algebraic implementation of ECMAScript 2015 and Promises/A+ Promises in JavaScript for as many browsers and Node.js versions as possible
JavaScript
71
star
3

riveted

A Clojure library for the fast processing of XML with VTD-XML.
Clojure
25
star
4

comp

A Ruby library to add function composition to Procs and Methods.
Ruby
24
star
5

python-delicious

A Python module to access del.icio.us via its API.
Python
23
star
6

jquery_placeholder

A jQuery plugin to support HTML5's placeholder attribute in older browsers.
JavaScript
15
star
7

homer

A lightweight DNS-over-HTTPS ("DOH") proxy written in Rust.
Rust
15
star
8

runspec.vim

A Vim plugin to run specs for the current file.
Ruby
12
star
9

fast_sessions

A fork of the Fast Rails Sessions plugin that is compatible with Rails 2.3.
Ruby
11
star
10

puppet-workstation

Attempting to automate the installation and configuration of my personal laptop with Puppet.
Ruby
9
star
11

php-clj

Deserialize PHP into Clojure data structures and back again.
Clojure
8
star
12

php-microkanren

A PHP implementation of ΞΌKanren.
PHP
8
star
13

oplog

A Rust library for iterating over a MongoDB replica set oplog.
Rust
7
star
14

fibonacci_heap

A Ruby implementation of the Fibonacci heap data structure.
Ruby
7
star
15

if

Implementing if in Ruby without using keywords.
Ruby
6
star
16

managing_web_application_servers_with_puppet

My presentation about Puppet for LRUG August 2011.
Shell
6
star
17

asset_compressor

A Rails plugin to compress stylesheets and JavaScript with the YUI Compressor.
Ruby
6
star
18

puppet-pkgin

A Puppet package provider for pkgin, a binary package manager for pkgsrc.
Ruby
5
star
19

atomic_page_caching

Rails plugin to add an atomic page caching method for ActionController.
Ruby
5
star
20

title_case

A Ruby implementation of John Gruber's Title Case.
Ruby
5
star
21

dotfiles

My various configuration files (.vimrc, etc.)
Vim Script
4
star
22

collapsing-puzzle

A simple Java puzzle game written in 2004 for a university project.
Java
4
star
23

blankable

A Ruby mixin to determine whether an object's values are blank with examples for Arrays and Hashes.
Ruby
4
star
24

jquery_clear

A simple jQuery plugin to clear all types of form inputs.
JavaScript
3
star
25

mudge.github.com

The source code for my blog.
JavaScript
3
star
26

tyger

A stylesheet for NetNewsWire (currently bundled with the app itself).
2
star
27

hubot-codenames

Suggesting solid gold, business appropriate names since 2013.
CoffeeScript
2
star
28

lego_bulldozer

An NQC program for the Lego RCX brick written for a course in 2004.
2
star
29

caesar.vim

A Vim plugin to convert numbers to Roman numerals used to demonstrate testing Vim script during Vim London, February 2013.
Ruby
2
star
30

mudgel

A toy programming language and an implementation of FizzBuzz
Ruby
2
star
31

foreclojure-downloader

A Clojure library to download 4clojure problems for offline work.
Clojure
1
star
32

readable

A generic way to create IO-like objects from any source
Ruby
1
star
33

prawn

A minimal Rails 2.3 plugin to provide a template handler for the Prawn PDF library.
Ruby
1
star
34

oplogjam

An experiment in writing a "safe" MongoDB oplog tailer in Ruby.
Ruby
1
star
35

new-turing-omnibus

Experiments while reading The New Turing Omnibus.
Ruby
1
star
36

nand2tetris

On-going solutions to the "From NAND to Tetris" exercises
Assembly
1
star
37

re2-test-action

GitHub Action to install Ruby and libre2-dev before running the re2 test suite.
Shell
1
star
38

configuration_management_with_puppet

My presentation for the first NTP Lightning Talks in April 2011
JavaScript
1
star
39

tapl

Bits & pieces as I read Benjamin C. Pierce's "Types and Programming Languages"
Rust
1
star
40

tableau

A pastie application written with Sinatra and Sequel.
Ruby
1
star
41

helloredis

An incomplete Ruby FFI interface to hiredis for educational purposes.
Ruby
1
star
42

sentient-isbn

An implementation of International Standard Book Numbers (both 10 and 13 digits) supporting conversion & correction using Sentient.
JavaScript
1
star
43

padding-oracles

A practical example of a padding oracle in Ruby
Ruby
1
star
44

spendthrift

A standalone iPhone web app for budgeting (currently an unstable work in progress).
JavaScript
1
star