2010-10-05 23:24:56 +00:00
|
|
|
/**
|
|
|
|
|
* HTML5 placeholder emulation for jQuery plugin
|
2010-11-22 23:55:37 +00:00
|
|
|
*
|
2010-10-05 23:24:56 +00:00
|
|
|
* This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
|
2010-11-22 23:55:37 +00:00
|
|
|
*
|
2010-10-05 23:24:56 +00:00
|
|
|
* @author Trevor Parscal <tparscal@wikimedia.org>
|
2010-11-19 00:12:43 +00:00
|
|
|
* @author Krinkle <krinklemail@gmail.com>
|
|
|
|
|
* @version 0.2.0
|
2010-10-05 23:24:56 +00:00
|
|
|
* @license GPL v2
|
|
|
|
|
*/
|
|
|
|
|
|
2011-01-10 05:33:03 +00:00
|
|
|
$.fn.placeholder = function() {
|
2010-11-19 00:12:43 +00:00
|
|
|
|
|
|
|
|
return this.each( function() {
|
|
|
|
|
|
|
|
|
|
// If the HTML5 placeholder attribute is supported, use it
|
|
|
|
|
if ( this.placeholder && 'placeholder' in document.createElement( this.tagName ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var placeholder = this.getAttribute('placeholder');
|
2011-01-10 05:33:03 +00:00
|
|
|
var $input = $(this);
|
2010-11-19 00:12:43 +00:00
|
|
|
|
|
|
|
|
// Show initially, if empty
|
|
|
|
|
if ( this.value === '' || this.value == placeholder ) {
|
|
|
|
|
$input.addClass( 'placeholder' ).val( placeholder );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$input
|
|
|
|
|
// Show on blur if empty
|
|
|
|
|
.blur( function() {
|
|
|
|
|
if ( this.value === '' ) {
|
|
|
|
|
this.value = placeholder;
|
|
|
|
|
$input.addClass( 'placeholder' );
|
|
|
|
|
} else {
|
|
|
|
|
$input.removeClass( 'placeholder' );
|
|
|
|
|
}
|
|
|
|
|
} )
|
|
|
|
|
|
|
|
|
|
// Hide on focus
|
2011-01-22 21:29:39 +00:00
|
|
|
.bind( 'onfocus ondrop ondragdrop', function() {
|
2010-11-19 00:12:43 +00:00
|
|
|
if ($input.hasClass('placeholder')) {
|
|
|
|
|
this.value = '';
|
|
|
|
|
$input.removeClass( 'placeholder' );
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
// Blank on submit -- prevents submitting with unintended value
|
|
|
|
|
this.form && $( this.form ).submit( function() {
|
|
|
|
|
// $input.trigger( 'focus' ); would be problematic
|
|
|
|
|
// because it actually focuses $input, leading
|
|
|
|
|
// to nasty behavior in mobile browsers
|
|
|
|
|
if ( $input.hasClass('placeholder') ) {
|
|
|
|
|
$input
|
|
|
|
|
.val( '' )
|
|
|
|
|
.removeClass( 'placeholder' );
|
2010-10-05 23:24:56 +00:00
|
|
|
}
|
2010-11-19 00:12:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
2011-01-22 21:29:39 +00:00
|
|
|
};
|