Skip to content

Instantly share code, notes, and snippets.

@nicholasruunu
Created November 2, 2011 15: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 nicholasruunu/1333858 to your computer and use it in GitHub Desktop.
Save nicholasruunu/1333858 to your computer and use it in GitHub Desktop.
jPholdr, Ultra Lightweight Placeholder Plugin for jQuery
/**
* jPholdr
* Ultra Lightweight Placeholder Plugin for jQuery
*
* @author: Nicholas Ruunu
* @email: nicholas@ruu.nu
* @website: http://ruu.nu
*
* Example: $('#form').jPholdr();
*/
if( ! 'placeholder' in document.createElement('input') )
{
(function($, undefined)
{
$.fn.jPholdr = function()
{
var $form = this;
var $inputs = $form.find('[placeholder]');
$form.on({
submit: function()
{
$inputs.triggerHandler('focus');
}
});
$inputs.on({
init: function()
{
if( this.type === 'password' )
{
this.type = 'text';
this.isPassword = true;
}
this.value = this.placeholder;
},
focus: function()
{
if( this.value === this.placeholder )
{
this.value = '';
if( this.isPassword !== undefined )
{
this.type = 'password';
}
}
},
blur: function()
{
if( ! this.value )
{
this.value = this.placeholder;
if( this.isPassword !== undefined )
{
this.type = 'text'
}
}
}
}).trigger('init');
}
}
})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment