| 1 |
// $Id$
|
| 2 |
|
| 3 |
( function( $ ) {
|
| 4 |
|
| 5 |
// plugin definition
|
| 6 |
$.fn.overlabel = function( options ) {
|
| 7 |
|
| 8 |
// build main options before element iteration
|
| 9 |
var opts = $.extend( {}, $.fn.overlabel.defaults, options );
|
| 10 |
|
| 11 |
var selection = this.filter( 'label[for]' ).map( function() {
|
| 12 |
|
| 13 |
var label = $( this );
|
| 14 |
var id = label.attr( 'for' );
|
| 15 |
var field = document.getElementById( id );
|
| 16 |
|
| 17 |
if ( !field ) return;
|
| 18 |
|
| 19 |
// build element specific options
|
| 20 |
var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;
|
| 21 |
|
| 22 |
label.addClass( o.label_class );
|
| 23 |
|
| 24 |
var hide_label = function() { label.css( o.hide_css ) };
|
| 25 |
var show_label = function() { this.value || label.css( o.show_css ) };
|
| 26 |
|
| 27 |
$( field )
|
| 28 |
.parent().addClass( o.wrapper_class ).end()
|
| 29 |
.focus( hide_label ).blur( show_label ).each( hide_label ).each( show_label );
|
| 30 |
|
| 31 |
return this;
|
| 32 |
|
| 33 |
} );
|
| 34 |
|
| 35 |
return opts.filter ? selection : selection.end();
|
| 36 |
};
|
| 37 |
|
| 38 |
// publicly accessible defaults
|
| 39 |
$.fn.overlabel.defaults = {
|
| 40 |
|
| 41 |
label_class: 'overlabel-apply',
|
| 42 |
wrapper_class: 'overlabel-wrapper',
|
| 43 |
hide_css: { 'text-indent': '-10000px' },
|
| 44 |
show_css: { 'text-indent': '0px', 'cursor': 'text' },
|
| 45 |
filter: false
|
| 46 |
|
| 47 |
};
|
| 48 |
|
| 49 |
} )( jQuery );
|