Use this script to automatically display hints on input textboxes in your forms. It uses the amazing jQuery library. I converted the script into a jQuery plugin called Form Input Hints to make it more flexible.
Update: I created an open source project on CodePlex to host the project. You can download the source with a demo there. I encourage anyone who would like to add to the functionality or change the code to do so in the project & commit your changes. I'll monitor the activity and create releases after testing.
Update: Now when the hint text is active, it adds a CSS class called “hint” to the textbox so you can style your hint text.
Check it out:
Usage:
<input type="text" title="Hint Text!" /> $(document).ready(function() { $('input[title]').inputHints(); });
Download from CodePlex: http://jqueryhints.codeplex.com (includes minified, debug, & demo)
Here is the Input Hints jQuery Plugin code:
// jQuery Input Hints plugin // Copyright (c) 2009 Rob Volk // http://www.robvolk.com jQuery.fn.inputHints=function() { // hides the input display text stored in the title on focus // and sets it on blur if the user hasn't changed it. // show the display text $(this).each(function(i) { $(this).val($(this).attr('title')) .addClass('hint'); }); // hook up the blur & focus return $(this).focus(function() { if ($(this).val() == $(this).attr('title')) $(this).val('') .removeClass('hint'); }).blur(function() { if ($(this).val() == '') $(this).val($(this).attr('title')) .addClass('hint'); }); };
Great !
ReplyDeleteThanks, save-me =D
This is very well done. Thank you for the hard work!
ReplyDeleteI tried loads of others and this just works, the others didn't, and it's the simplest script too!
ReplyDeleteThanks so much
A simple solution. Thank you very much.
ReplyDeleteBrilliant! Super simple and light weight.
ReplyDeleteTHANKS!!!
I have taken the liberty to making a slight mod to your plugin (which works quite nicely - thanks) to provide namespace protection. $ conflicts with Prototype.
ReplyDelete// jQuery Input Hints plugin
// Copyright (c) 2009 Rob Volk
// http://www.robvolk.com
(function($){$.fn.inputHints=function(){$(this).each(function(i){$(this).val($(this).attr('title'));});$(this).focus(function(){if($(this).val()==$(this).attr('title'))
$(this).val('');}).blur(function(){if($(this).val()=='')
$(this).val($(this).attr('title'));});};})(jQuery);
Wonderful. So simple, thanks :)
ReplyDeleteI created an open source project for my jQuery Form Input Hints plugin so we can keep it alive and add features. I'll merge the contributions in the blog comments as well.
ReplyDeletehttp://jqueryhints.codeplex.com
just posted my code in a discussion on codeplex. The code will remove the hint before the form is submitted. also adds some spaces to the title just in case someone tries to enter the exact title text.
ReplyDeleteThanks Rob...
ReplyDeleteI had modified a bit to include some color things and select the text. Below is the javascript code:
// show the display text
$(this).each(function(i) {
$(this).val($(this).attr('title'));
}).addClass('blur');
// hook up the blur & focus
return $(this).focus(function() {
if ($(this).val() == $(this).attr('title'))
$(this).val('').removeClass('blur').addClass('focus');
else
$(this).select();
}).blur(function() {
if ($(this).val() == '')
$(this).val($(this).attr('title')).removeClass('focus').addClass('blur');
});
and this the css code:
<style type="text/css">
input.blur {
color: #999;
border: 1px ridge #c0c;
}
input.focus {
color: #000;
border: 1px ridge #c0c;
background-color:#ffc;
}
</style>
thanks! would you mind submitting your changes to the project via CodePlex? http://jqueryhints.codeplex.com/
ReplyDeletedemo doesn't appear to work? no hint text shown in field "Check it out"
ReplyDeleteHello Rob...
ReplyDeleteI´ve made some modifications to the script.
I put it here: http://jqueryhints.codeplex.com/Thread/View.aspx?ThreadId=213075
Thanks!
Wasted link!
ReplyDeletenice
ReplyDeletehttp://java.pakcarid.com//default.aspx?sub=25&Sls=25
amazing i really love this
ReplyDeleteI made a slight modification, which removes the hint when the form is submitted.
ReplyDelete$(document).ready(function() {
//Shows the title in the text box, and removes it when modifying it
$('input[title]').each(function(i) {
$(this).val($(this).attr('title'))
.addClass('hint');
$(this).focus(
function() {
if ($(this).val() == $(this).attr('title')) {
$(this).val('')
.removeClass('hint');
}
}
);
$(this).blur(
function() {
if ($(this).val() == '') {
$(this).val($(this).attr('title'))
.addClass('hint');
}
}
);
});
//Clear input hints on form submit
$('form').submit(function() {
$('input.hint').val('');
return true;
});
});
// show the display text
ReplyDelete$(this).each(function(i) {
** if($(this).val() == '')
$(this).val($(this).attr('title'))
.addClass('hint');
});
start value...
ITs not working....... :'( :'(
ReplyDeleteHow could you use this for a textarea instead of a textbox?
ReplyDeleteIt should work the same. does this not work?
ReplyDelete$('textarea[title]').inputHints()