changing implied if-statement into a real if-statment; Passes strict settings of...
[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 if ( this.form ) {
51 $( this.form ).submit( function() {
52 // $input.trigger( 'focus' ); would be problematic
53 // because it actually focuses $input, leading
54 // to nasty behavior in mobile browsers
55 if ( $input.hasClass( 'placeholder' ) ) {
56 $input
57 .val( '' )
58 .removeClass( 'placeholder' );
59 }
60 });
61 }
62
63 });
64 };
65 } )( jQuery );