«
»

jQuery, user interface

Clear and re-fill form fields using jQuery

17.06.09 | 1 Comment

This piece of code makes form fields more intelligent and usable. It clears the field on focus and if nothing is filled-in, it puts back the default value. There is a single line function MooTools

for this, but I used jQuery elsewhere on the web, so I needed jQuery solution. But couldn’t find any good one, so I put together my own.

The form can be seen in action at http://www.fofrem.info/objednat.html

Usage:

Just put the default text into alt attribute of the text field, like this:


For some reason, I couldn’t make it work by using value attribute. The if($(this).val()==$(this).attr("value")) part doesn’t work. Anyone knows why?

The script will do the rest. It is pretty obvious from the code how it works, even if you know little about jQuery.



Please don’t hesitate to leave a comment if you need more details. Enjoy!



1 Comment

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


«
»