|
|
|
DailyCoding > Design
|
"Default Text" Fields Using Simple jQuery Trick
|
| Explains how to implement default text input field using simple jQuery trick |
|
Author
admin on
Aug 1, 2008 |
21 Comments
|
| Rate it |
|
(Rated
4
by
38
people)
|
 |
Loading.. |
|
13,815 Views
|
|
|
A lots of the time you want to add instructions to text box fields of the page to provide usability information to the users. Alternate way of doing this could be using default text in the input fields. A default text is the text which appears in the text box when it is empty. e.g.
You can see this is useful and good idea to have this in the UI. There is an easy trick in jQuery to do this. Below is the step by step procedure to do this.
Step1 If you don't have jQuery then download latest from its website and include in you page.<script src="jquery.js" type="text/javascript"></script>
Step 2 Create two CSS classes defaultText and defaultTextActive as mentioned below.<style media="screen" type="text/css">
.defaultText { width: 300px; }
.defaultTextActive { color: #a1a1a1; font-style: italic; }
</style>
Step 3 Add this JavaScript to you page<script language="javascript">
<!--
$(document).ready(function()
{
$(".defaultText").focus(function(srcc)
{
if ($(this).val() == $(this)[0].title)
{
$(this).removeClass("defaultTextActive");
$(this).val("");
}
});
$(".defaultText").blur(function()
{
if ($(this).val() == "")
{
$(this).addClass("defaultTextActive");
$(this).val($(this)[0].title);
}
});
$(".defaultText").blur();
});
//-->
</script>
Step 4 To every text field or text area where you want to apply default text add "defaultText" as css class and specify the default text in the title attribute. e.g.<input class="defaultText" title="e.g. someone@example.com" type="text" />
You default text setup is ready. Run the page and see the magic. Click here for a running sample page.
|
|
CSS
|
Html
|
Javascript
|
jQuery
|
Utility
|
|
|
|
Bill
|
On Aug 2, 2008 12:34 PM
|
|
|
|
|
Bill
|
On Aug 2, 2008 12:43 PM
|
|
|
|
|
SeanJA
|
On Aug 3, 2008 10:08 AM
|
|
|
|
|
SeanJA
|
On Aug 3, 2008 10:09 AM
|
|
|
|
|
SeanJA
|
On Aug 3, 2008 10:11 AM
|
|
|
|
|
Alex
|
On Aug 13, 2008 01:58 AM
|
|
|
|
|
|
Vaibhav Gupta
|
On Sep 12, 2008 01:08 AM
|
|
|
|
|
Rubist
|
On Feb 10, 2009 06:22 AM
|
|
|
|
|
Andrew
|
On Mar 21, 2009 03:15 PM
|
|
|
|
|
kav
|
On Mar 21, 2009 06:01 PM
|
|
|
|
|
lewis
|
On Mar 24, 2009 07:29 AM
|
|
|
|
|
,m.,m
|
On Mar 31, 2009 03:05 AM
|
|
|
|
|
HeavyP
|
On Apr 28, 2009 01:45 PM
|
|
|
|
|
Leandro Santana Pereira
|
On May 3, 2009 07:23 PM
|
|
|
|
|
Rinke van den Berg
|
On May 20, 2009 11:11 PM
|
|
|
|
|
Niyaz
|
On May 30, 2009 07:46 AM
|
|
|
|
|
leo
|
On Aug 10, 2009 10:32 PM
|
|
|
|
|
Jordi
|
On Nov 25, 2009 11:13 AM
|
|
|
|
|
Anton Venema
|
On Jan 4, 2010 01:46 PM
|
|
|
|
|
Anton Venema
|
On Jan 4, 2010 01:47 PM
|
|
|
|
Leave a Comment
|
 |
Loading.. |
|
|
|
|
|