jquery.fieldtag.js – watermarks for inputfields
Description

Nifty watermarking and styling.
Did you ever see some input fields that have a description tag in it and as soon as you click into them, in order to write something, the description disappears? That’s exactly what this jQuery plugin does for you. Even picture watermarks are a piece of cake, in fact everything handled by a string of Text and/or a CSS-class assigned to an input field, is possible. For more creative ideas, just visit the demo page.
How do I use it?
This plugin works with jQuery 1.3.2+ (it should also work with earlier versions, but this is untested). Proper loading of a jQuery plugin is explained in my previous post.
As soon as the document is ready, just call $(".selector .of input").fieldtag()
To make use of the appended CSS class feature, while the field is tagged, you just have add some styles to your stylesheet:
input{ /*this are your existing styles*/ } input.tagged{ /*appended while tagged, for example background-image*/ }
Sometimes you want to set the text manually, this is done by calling
$("input").fieldtag({ standardText:"Your text" })
Demo and source
- demo
- jquery.fieldtag.js - source (1.8kB)
- jquery.fieldtag.min.js - minified version (1.4kB), shrunk by jsmin
As jQuery itself, this plugin released under dual MIT/GPL license.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | /* jQuery Fieldtag Plugin * Version 1.1 * 2009-05-07 10:10:35 * URL: http://ajaxcssblog.com/jquery/fieldtag-watermark-inputfields/ * Description: jQuery Plugin to dynamically tag an inputfield, with a class and/or text * Author: Matthias Jäggli * Copyright: Copyright (c) 2009 Matthias Jäggli under dual MIT/GPL license. * * Changelog * 1.1 * Support for proper clearing while submitting the form of tagged fields * 1.0 * Initial release */ (function($){ $.fn.fieldtag = function(options){ var opt = $.extend({ markedClass: "tagged", standardText: false }, options); $(this) .focus(function(){ if(!this.changed){ this.clear(); } }) .blur(function(){ if(!this.changed){ this.addTag(); } }) .keyup(function(){ this.changed = ($(this).val()? true : false); }) .each(function(){ this.title = $(this).attr("title"); //strange IE6 Bug, sometimes if($(this).val() == $(this).attr("title")){ this.changed = false; } this.clear = function(){ if(!this.changed){ $(this) .val("") .removeClass(opt.markedClass); } } this.addTag = function(){ $(this) .val(opt.standardText === false? this.title : opt.standardText ) .addClass(opt.markedClass); } if(this.form){ this.form.tagFieldsToClear = this.form.tagFieldsToClear || []; this.form.tagFieldsToClear.push(this); if(this.form.tagFieldsAreCleared){ return true; } this.form.tagFieldsAreCleared = true; $(this.form).submit(function(){ $(this.tagFieldsToClear).each(function(){ this.clear(); }); }); } }) .keyup() .blur(); return $(this); } })(jQuery); |
Comments(31)
was passiert mit der Value beim Post?
Man müsste es ja dann quasi wieder leeren bevor man den post macht.
so quasi:
function checkinputbeforesubmit(){
var el = $(’input[title]‘);
el.each(function(i) {
if ($(this).attr(’value’) == $(this).attr(’title’))
$(this).attr(’value’, ”);
});
}
@FJ: Thank you to pointing out that the fields will contain the tagged value, while submitting the corresponding form. Now the fields are cleared proper, during submitting.
Yey. thanks. I’ll test it next week.
The demo is not working…
Under Ie or firefox
Antony
@HOTEL can you be more precise, please? I can’t figure out any problems..
I really like the demo with a predefined value this will come in really handy for giving clues when users shouldn’t leave a field blank. Everything is working fine for me in both IE and Firefox and i’ve tried it on a number of versions
i like this script
[...] DOWNLOAD: http://ajaxcssblog.com/jquery/fieldtag-watermark-inputfields/ [...]
Love this wonderful script. Thanks..
This plugin breaks down under multiple refreshes (even on the demo page). Best implementation I’ve seen, but it just doesn’t work after a refresh or two. Anyone here know how to fix this problem?
@John
I think you tested it with firefox (or similar) and didn’t hit CTRL+F5 to refresh. Firefox does not clear formfields by a regular refresh. If so, this was a feature, not a bug, because formfields may be prefilled by the server.
If you still can reproduce this issue with CTRL+F5, please let me know.
[...] jQuery Watermarks for Inputfields [...]
[...] jQuery Watermarks for Inputfields [...]
[...] jQuery Watermarks for Inputfields [...]
How do I clear the field tag values before I submit the form?
@brian
You do not have to clear them manually. On submit, default values will be cleared automatically.
Hi mat,
Thank you so much for this wonderful script. It works great. cheers!!
Thanks for a great script – much better than others I’ve tried as they always send the watermark text as the form value when posting.
However, I’m also using the jQuery validate plugin which highlights any missing fields. It works well with yours as the title values ARE cleared before the submit.. Yet, if the form fails validation all I end up with is the blank boxes and no watermarked text in the invalid fields.
Is there a call I can make to re-test and re-populate the text boxes with the watermarked values?
Hope that makes sense…
Martin
It shows different output in IE6 & IE7 on combination of text and image demo.
Using this plug-in with the jQuery datepicker and I ended up adding a hook for the Change event to get everything to work properly.
Thanks for the good work!
i like this script
[...] Ploneのテキストフィールドのデフォルト文字 1月 27, 2010 3:59 pm ngi644 Zope/Plone テキストフィールド欄にデフォルト文字を表示させたいとき、一般的には、jquery.fieldtab.jsを使う。 [...]
this is the first watermark code i found that actually WORKED!!!
thanks so much for sharing, what a champ!
If anyone runs into an issue where in some browsers hitting refresh or the back button causes the plugin not to work (because the browser is pre-populating the fields with the watermarked value) you can fix it by changing the keyup event to this:
.keyup(function(){
this.changed = ($(this).val() && $(this).val() !== $(this).attr(’title’) ? true : false);
})
Otherwise works like a charm!
doesn’t work in Password fields, i’m getting asterisks!!
You can use the following code to clear and submit the form via javascript:
Change to jquery.fieldtag.js:
if (this.form) {
this.form.tagFieldsToClear = this.form.tagFieldsToClear || [];
this.form.tagFieldsToClear.push(this);
if (this.form.tagFieldsAreCleared) { return true; }
this.form.tagFieldsAreCleared = true;
$(this.form).submit(function () {
$(this.tagFieldsToClear).each(function () {
this.clear();
});
});
/* BEGIN-CHANGE: Added Method to form for submitting form but clearing first*/
this.form.clearAndSubmit = function () {
$(this.tagFieldsToClear).each(function () {
this.clear();
});
this.submit();
};
/* END-CHANGE */
}
An example of a drop down list posting a form using the clearAndSubmit method:
None
One
Two
Three
Some strange encoding removed my drop down list code:
here is what the onchange event looks like:
onchange=”this.form.clearAndSubmit();”>
How can I get this to work with password input fields? I’m not a jquery expert… any help would be much appreciated!
[...] watermarks for inputfields [...]
[...] watermarks for inputfields [...]
[...] watermarks for inputfields [...]