hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Programming Tutorials : HTML input fields with default values using jQuery
Reply

Programming Tutorials How-Tos related to programming, databases, and the like.
Forum Jump

HTML input fields with default values using jQuery

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 10-03-2011, 12:10 PM
j.vihavainen j.vihavainen is offline
Newbie
 
Join Date: Mar 2004
Posts: 13

HTML input fields with default values using jQuery


Designers often choose to compact their designs by putting input field labels in to the input boxes themselves. It is our jobs as developers then to make the default label text disappear when a user clicks or focuses on the input box. jQuery makes this super simple. All it takes is a few lines of javascript and a special class name for your input fields.

The HTML:

HTML Code:
<input type="text" name="email" value="Email" class="jsClearDefault" />
<input type="password" name="password" value="Password" class="jsClearDefault" />
<input type="submit" name="action" value="Login" />
The Javascript:

HTML Code:
<script type="text/javascript">

$('.jsClearDefault').each(function(i){
    var defaultValue = $(this).val();

    $(this).focus(function(){
        if($(this).val() == defaultValue)
        {
            $(this).val('');
        }
    });

    $(this).blur(function(){
        if($(this).val().length == 0)
        {
            $(this).val(defaultValue);
        }
    });
});

</script>
All you have to do to get this working on your page is paste the Javascript block right before your </body> tag and give your input fields the jsClearDefault class. You also need to have jQuery loaded on your page within the <head> of your document. You can load it directly from Google using:

HTML Code:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
I have hosted a sample of this script in action HERE.

Reply With Quote


Sponsored Links
  #2  
Old 10-05-2011, 03:32 PM
HostGrenade HostGrenade is offline
Junior Guru Wannabe
 
Join Date: Sep 2011
Posts: 69
Nice, but the one for the password is unneeded. Changing the input type doesn't work either.

Code:
<input type="password" name="password" style="display:hidden" value=""/>
<input type="text" name="pass" value="Password" class="jsClearDefault"/>
Than, the jQuery code

Code:
$('.jsClearDefault').focus(function(){
    var value = $(this).val();
    if($(this).attr('type') == 'text' && $(this).attr('name') == 'pass'){
        $(this).hide().prev().show().focus();
    }.blur(function(){
        if($(this).attr('name') == 'password' && $(this).attr('type') == 'password')){
        $(this).hide().next().val(value).show();
}
    })    
})
Of course, when the form is sent, you'll have to disregard the input[name="pass"], also this does not take into account the jQuery code from before. Code should work

Reply With Quote
  #3  
Old 01-10-2012, 01:38 PM
j.vihavainen j.vihavainen is offline
Newbie
 
Join Date: Mar 2004
Posts: 13
HostGator,

Naturally people have different requirements and preferences, but I don't think there is anything wrong in my code sample. I have the same script deployed on production with a couple different websites. It also works in the sample link that I posted.

Quote:
Nice, but the one for the password is unneeded.
Why is it unneeded?

Reply With Quote
Sponsored Links
  #4  
Old 01-12-2012, 05:59 PM
HostGrenade HostGrenade is offline
Junior Guru Wannabe
 
Join Date: Sep 2011
Posts: 69
Wrong company but close/

Having a default value of "Password" for a password input will result in a whole bunch of bullets, which is pointless.

Mine will show "Password" and on focus will switch to the actual password input.

Reply With Quote
  #5  
Old 01-12-2012, 06:23 PM
8088 8088 is offline
Web Hosting Master
 
Join Date: Nov 2010
Posts: 616
Quote:
Originally Posted by HostGrenade View Post
Having a default value of "Password" for a password input will result in a whole bunch of bullets, which is pointless.
People usually associate bullets with a password field. Changing from visible text to bullets is rather confusing and non-intuitive.
Quote:
Mine will show "Password" and on focus will switch to the actual password input.
Not if JavaScript is turned off. And it will also prevent browsers from offering the user to remember the password.

Still, using default values is pointless in my opinion. I'd rather use HTML labels. Nice tutorial though, j.vihavainen.

Reply With Quote
  #6  
Old 01-17-2012, 02:36 PM
j.vihavainen j.vihavainen is offline
Newbie
 
Join Date: Mar 2004
Posts: 13
Quote:
Originally Posted by 8088 View Post
Still, using default values is pointless in my opinion. I'd rather use HTML labels. Nice tutorial though, j.vihavainen.
I agree that default values as labels is not the best way to go, but sometimes you can't bend the specs to your will.

Reply With Quote
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with a php mysql form that has two array input fields mjfroggy Programming Discussion 1 09-02-2011 01:10 PM
User input values and SSL hotdog1983 Hosting Security and Technology 7 10-29-2010 08:59 AM
jquery + text input and image upload hello_x Programming Discussion 1 07-21-2009 12:48 PM
Wierd whitespace added in text input fields??? seodevhead Programming Discussion 1 11-10-2005 05:26 PM
background color of input fields sasha Programming Discussion 2 05-19-2004 07:02 PM

Related posts from TheWhir.com
Title Type Date Posted
Mozilla Firefox 23 Will Block Mix SSL Content by Default Web Hosting News 2013-04-12 11:39:07
Mozilla Updates CA Certificate Policy for Software Products Web Hosting News 2013-02-19 12:32:22
ITX Design Launches Service to Help Simplify PCI DSS Compliance Web Hosting News 2013-01-02 10:59:36
Hurricane Electric Offers Online Courses for Developers and Designers Web Hosting News 2011-11-14 21:51:44
ICANN CEO to Depart, Board Begins Search Process for Successor Web Hosting News 2011-10-31 13:55:55


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?