Wrapping core modules (FIXME from r79929)
[lhc/web/wiklou.git] / resources / jquery / jquery.placeholder.js
1 /**
2 * HTML5 placeholder emulation for jQuery plugin
3 *
4 * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
5 *
6 * @author Trevor Parscal <tparscal@wikimedia.org>
7 * @author Krinkle <krinklemail@gmail.com>
8 * @version 0.2.0
9 * @license GPL v2
10 */
11 ( function( $ ) {
12
13 $.fn.placeholder = function() {
14
15 return this.each( function() {
16
17 // If the HTML5 placeholder attribute is supported, use it
18 if ( this.placeholder && 'placeholder' in document.createElement( this.tagName ) ) {
19 return;
20 }
21
22 var placeholder = this.getAttribute('placeholder');
23 var $input = $(this);
24
25 // Show initially, if empty
26 if ( this.value === '' || this.value == placeholder ) {
27 $input.addClass( 'placeholder' ).val( placeholder );
28 }
29
30 $input
31 // Show on blur if empty
32 .blur( function() {
33 if ( this.value === '' ) {
34 this.value = placeholder;
35 $input.addClass( 'placeholder' );
36 } else {
37 $input.removeClass( 'placeholder' );
38 }
39 } )
40
41 // Hide on focus
42 .focus( function() {
43 if ($input.hasClass('placeholder')) {
44 this.value = '';
45 $input.removeClass( 'placeholder' );
46 }
47 } );
48
49 // Blank on submit -- prevents submitting with unintended value
50 this.form && $( this.form ).submit( function() {
51 // $input.trigger( 'focus' ); would be problematic
52 // because it actually focuses $input, leading
53 // to nasty behavior in mobile browsers
54 if ( $input.hasClass('placeholder') ) {
55 $input
56 .val( '' )
57 .removeClass( 'placeholder' );
58 }
59 });
60
61 });
62 };
63 } )( jQuery );