Blog of Jeff A blog about programming and random other things.

JQuery Blank

Version: 1.3

JQuery Blank is a simple way to create default values for text fields and textareas which hints to the user what should be filled into the field.

defsearchbox

When the user focuses on the textbox, the search text is instantly removed and the user can then type something. If the user leaves the textbox and if the textbox is still empty, the default text is restored.

Simpliest usage:

$(myelement).blank('Search');

Documentation:

jQuery.fn.blank([default_text[, options]]);

  • Default Text: The default text to display for the given element.
  • Options: Additional parameters to pass: (object)
    • defaultCSS: The css style when the text input field is empty. This can be either a object (hash) or a function call that returns an object. The function contains the element as this and as the first parameter. The default value is {color: '#999'}.
    • normalCSS: The css style when the text has entered user input. This can also be either an object or a function that returns one. The parameters passed to the function are the element, the object of defaultCSS, and the object of d. The element is also passed as this. The default is a function that merges both defaultCSS and defaultFocusCSS for style attributes and computates the style values for the element when it is called.
    • defaultFocusCSS: The css style when the text input field has user focus, but is still blank (user didn't type anything yet). This is used to for a similar effect like Apple's me.com login box. This style only applies when defaultUntilType is set to true. Defaults to {color: '#bbb'}.
    • defaultUntilType: If enabled, when the user focuses on the text input, the defaultFocusCSS style is applied and the default text is display until the user types something. Once the user types something, the default text is removed and the user can type input.

If no parameters are specified, then the function returns whether or not the text field is blank. True if the input text is blank, false if not.


$('#myform').submit(function(){
  if($('#myinput').blank()){
    alert('Please fill out the input box!');
    return false;
  }
  return true;
});

You can get it here or check out a demo.

  • Share/Bookmark
  • I'm back!

    I added some logic to deal with password fields. We kept using this to hint email/password for login fields and had to keep manually switching a text input out for a password one so I decided just to add the logic into the blank() function. http://pastie.org/939405

    Basically, if you try and blank() a password field, it will insert a text input right next to the password node, blank that, and set up some event handlers to deal with hiding and showing the password field at the right time.
  • Nevermind previously, I've completed it. You can find it at http://github.com/jeffh/jquery.blank I'm planning to push a site redesign sometime near the end of May.
  • sweet!
  • Hello again!

    I'd love to upload the changes, but it unfortunately it doesn't quite work
    for *defaultUntilType* option. I'll add it and try working on getting
    something for that option too before releasing it.

    Also, placing your password code inside the *return this.each(function(){});
    * is probably a better idea - so it works on every element selected.

    Thanks for the code, I'll try getting a new release out when I get the time
    [computer graphics final project is crushing me :( ]
  • Hey man-
    Great plugin. One thing though, if the inputs are inside a form the default "blank" text is submitted. This is sort of a pain since it breaks our search business.

    I added the follow to sort it out:

    return this.each(function(){
    var d = call(opt.defaultCSS, this);
    var df = call(opt.defaultFocusCSS, this, d);
    var n = call(opt.normalCSS, this, d, df);
    var el = $(this);
    if(!el.is('input[type=text], textarea'))
    return true; // continue

    // ashish@setfive.com
    // if the element is inside a form don't submit the blank text
    var myForm = $(this).closest("form");
    if( myForm ){
    myForm.submit( function(){
    if( el.val() == text ){
    el.val("");
    }
    return true;
    });
    }

    if(!opt.defaultUntilType){
    el.blur(function(){
    if($.trim(el.val()) === '')
  • Thanks, Ashish. I've added your changes in the latest version.
  • Hey there! This is a great script, as the new defaultUntilType gets around the usual hint bug where the hint text becomes real text when you hit "reload" on the browser.

    The only problem I found is that you get an error in IE (versions 6 and 7) when using defaultUntilType. I debugged it, and it's happening because IE doesn't understand indexOf. If you add the script found here (you may want to include it in your script), it works like a charm: http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/

    Thanks!

    --Sherri
  • Thanks for pointing that bug out. :)
    Turns out jquery has a built-in function to do this,
    jQuery.inArray<http: docs.jquery.com="" jquery.inarray#valuearray="" utilities="">,
    so I'm just utilizing that.

    - Jeff</http:>
blog comments powered by Disqus

Recent Posts

Topics

Archives

Following

Links