Skip to content

Instantly share code, notes, and snippets.

@axiak
Created April 17, 2011 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axiak/923630 to your computer and use it in GitHub Desktop.
Save axiak/923630 to your computer and use it in GitHub Desktop.
/*
* jQuery placeholder Plugin
* version: 0.1
* @requires: jQuery v1.4.1 or later, Modernizr, and jquery livequery plugin
*
* http://docs.jquery.com/Plugins/livequery
*
* To use: When you write your input tags, include a title and placeholder attribute as
* well as a placeholder class. Also include styling for the hasPlaceholder class.
*
* E.g.:
* CSS:
* .hasPlaceholder { color: #ddd; font-style: italic; }
*
* <input type="text" class="placeholder" title="Username" placeholder="Username" name="username">
*/
if (!Modernizr.input.placeholder) {
$(function() {
var blurInputs = function (selector) {
selector.each(function () {
var trimmedTitle = $.trim(this.title), trimmedValue = $.trim(this.value);
if (trimmedTitle !== '' && (trimmedValue === '' || trimmedValue == trimmedTitle)) {
this.value = this.title;
$(this).addClass('hasPlaceholder');
}
});
};
$(':text.placeholder')
.livequery(function () {
blurInputs($(this));
})
.live('focus', function () {
if ($.trim(this.title) !== '' && $.trim(this.value) === $.trim(this.title)) {
this.value = '';
$(this).removeClass('hasPlaceholder');
}
})
.live('blur', function () {
blurInputs($(this));
});
$("form").live("submit", function () {
$(this).find(':text.hasPlaceholder').val('');
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment