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-31 19:33:16 +00:00
|
|
|
( function( $ ) {
|
2010-10-05 23:24:56 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-24 23:10:04 +00:00
|
|
|
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
|
2011-02-24 23:10:04 +00:00
|
|
|
if ( this.value === '' || this.value === placeholder ) {
|
2010-11-19 00:12:43 +00:00
|
|
|
$input.addClass( 'placeholder' ).val( placeholder );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$input
|
|
|
|
|
// Show on blur if empty
|
|
|
|
|
.blur( function() {
|
|
|
|
|
if ( this.value === '' ) {
|
|
|
|
|
this.value = placeholder;
|
|
|
|
|
$input.addClass( 'placeholder' );
|
|
|
|
|
}
|
|
|
|
|
} )
|
|
|
|
|
|
|
|
|
|
// Hide on focus
|
2011-03-09 13:54:02 +00:00
|
|
|
// Also listen for other events in case $input was
|
|
|
|
|
// already focused when the events were bound
|
|
|
|
|
.bind( 'focus drop keydown paste', function( e ) {
|
2011-02-24 23:10:04 +00:00
|
|
|
if ( $input.hasClass( 'placeholder' ) ) {
|
2011-03-09 13:54:02 +00:00
|
|
|
if ( e.type == 'drop' && e.originalEvent.dataTransfer ) {
|
2011-03-09 14:27:13 +00:00
|
|
|
// Support for drag&drop. Instead of inserting the dropped
|
|
|
|
|
// text somewhere in the middle of the placeholder string,
|
|
|
|
|
// we want to set the contents of the search box to the
|
|
|
|
|
// dropped text.
|
2011-08-12 21:48:10 +00:00
|
|
|
|
2011-03-09 14:27:13 +00:00
|
|
|
// IE wants getData( 'text' ) but Firefox wants getData( 'text/plain' )
|
|
|
|
|
// Firefox fails gracefully with an empty string, IE barfs with an error
|
|
|
|
|
try {
|
|
|
|
|
// Try the Firefox way
|
|
|
|
|
this.value = e.originalEvent.dataTransfer.getData( 'text/plain' );
|
|
|
|
|
} catch ( exception ) {
|
|
|
|
|
// Got an exception, so use the IE way
|
|
|
|
|
this.value = e.originalEvent.dataTransfer.getData( 'text' );
|
|
|
|
|
}
|
2011-08-12 21:48:10 +00:00
|
|
|
|
2011-03-09 14:27:13 +00:00
|
|
|
// On Firefox, drop fires after the dropped text has been inserted,
|
|
|
|
|
// but on IE it fires before. If we don't prevent the default action,
|
|
|
|
|
// IE will insert the dropped text twice.
|
|
|
|
|
e.preventDefault();
|
2011-03-09 13:54:02 +00:00
|
|
|
} else {
|
|
|
|
|
this.value = '';
|
|
|
|
|
}
|
2010-11-19 00:12:43 +00:00
|
|
|
$input.removeClass( 'placeholder' );
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
// Blank on submit -- prevents submitting with unintended value
|
2011-02-24 23:10:04 +00:00
|
|
|
if ( 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-11-19 00:12:43 +00:00
|
|
|
|
|
|
|
|
});
|
2011-01-22 21:29:39 +00:00
|
|
|
};
|
2011-03-03 18:04:11 +00:00
|
|
|
} )( jQuery );
|