Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a jQuery plugin for fading textbox hints?

I was wondering whether there is a jQuery plugin available which can show hints in a textbox when it is empty.

What I found was: http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/. However, this just acts as the placeholder HTML5 attribute. What I'm looking for is rather a plugin that shows multiple hints with fading, as on http://www.wolframalpha.com/. (Edit: I mean the grey text in the textbox - not the tooltip.)

Although it might not be too much trouble to create it myself, I don't subscribe to the theory of reinventing the wheel - so does anyone know if such a plugin is already available?

Thanks.

like image 346
pimvdb Avatar asked Nov 14 '22 01:11

pimvdb


1 Answers

I made one up myself, so that it fits my needs completely: http://jsfiddle.net/42t6R/2/.

It's simple but it works just nicely.

Edit: New version which has less bugs, also why not submit it as a plugin :)

http://plugins.jquery.com/project/fadehints

http://jsfiddle.net/9rgHg/2/

(function( $, undefined ) {

    $.fn.fadehints = function( data, speed ) {
        var i = 0;
        var $this = $( this );
        var offset = $this.offset();
        var $input = $("<input>").css( "position",          "absolute"                 )
                                 .css( "left",              offset.left                )
                                 .css( "top",               offset.top                 )
                                 .css( "background-color",  "transparent"              )
                                 .css( "color",             "gray"                     )
                                 .css( "border",            0                          )
                                 .css( "padding",           2                          )
                                 .css( "font-size",         $this.css( "font-size"   ) )
                                 .css( "font-family",       $this.css( "font-family" ) );

        var $parent = $this.parent();
        var $div = $( "<div>" ).append( $this.detach(), $input );

        var change = function() {
            if( i >= data.length ) {
                i = 0;
            }
            $input.hide().val( data[i] ).fadeIn( 1000 );
            i++;
        };

        $this.bind( "focus keydown", function(e) {
            if( !( e.bubbles == null ) ) { // Only clear if event was triggered by user
                window.clearInterval( interval );
                $input.hide();
            }
        } );

        $input.bind( "click focus", function() {
            window.clearInterval( interval );
            $this.focus(); // $this === the real textbox
            $( this ).hide(); // $(this) === the overlap textbox
        } );

        $this.click( function() {
            $input.hide();
            window.clearInterval( interval );
        } );

        $this.blur( function() {
            window.clearInterval( interval );
            if( $this.val() === "" && $this[0] !== document.activeElement ) {
                if( !$input.is(":visible")) {
                change();
                }
                interval = window.setInterval( change, speed );
            }
        } );
        $parent.append( $div );

        change(true);
        var interval = window.setInterval( change, speed );


        return $this;
    };

})(jQuery);

$(function() {
    $('#tb').fadehints([
        "test1", "test2"
    ]);
});
like image 118
pimvdb Avatar answered Dec 18 '22 16:12

pimvdb