jQuery form titles

jQuery form titles

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on July 7, 2010

I thought I’d share a little technique that I occasionally use when coding forms with a limited amount of space. It’s a simple effect that allows you to contain the form input or textarea title within the form allowing the user to click inside the form input to remove it. You often see this used on search fields

The JavaScript

firstly we assign a class of ‘form-field’ as well as adding the attribute ‘defaultVal’ with the text that we want to display in our input field.

The code below simply adjusts the input value attribute depending on if the user click in the field and types text so we have a state of change for ‘focus’ and ‘blur’.

$(document).ready(function(){

  $('.form-field').each( function () {
    $(this).val($(this).attr('defaultVal'));
    $(this).css({color:'#ccc'});
      });

  $('.form-field').focus(function(){
    if ( $(this).val() == $(this).attr('defaultVal') ){
      $(this).val('');
      $(this).css({color:'#000'});
    }
    });
  $('.form-field').blur(function(){
    if ($(this).val() == '' ){
      $(this).val($(this).attr('defaultVal'));
      $(this).css({color:'#ccc'});
    }
    });

});

The HTML

 <form method="post" action="">

	    <input type="text" class="form-field" name="Name" defaultVal="Name" value="" />
            <input type="text" class="form-field" name="Phone" defaultVal="Phone Number" value="" />
            <input type="text" class="form-field" name="Email" defaultVal="Email address" value="" />
            <input type="text" class="form-field" name="Url" defaultVal="Site URL" value="" />

	</form>

More tutorials from Papermashup
Comments
11 discussions around jQuery form titles
  1. scannermobs says:

    Brilliant stuff! a validation-passing version would be great if you figure it out. Cheers for all your work

  2. chrisgun says:

    How can i cange this with a password field, that it shows “Password” when there is nothing writen in the Field and then “***********” when the User enters something?

  3. yassi says:

    this post defaultVal not form value.

  4. Ann says:

    It would be even better if you could use actual “label” tag value to do that. This way the form would be more accessible.

  5. yara says:

    When you submit form the string into the input field is changed by defaultVal string.

  6. rohnn says:

    My main issue with this technique is the use of an attribute that is not actually an attribute.

    defaultVal obviously does not validate. :)

    as for HTML5 placeholder… very handy indeed, but I believe my client’s requirement of IE6+ compliant site is to be forgotten !

  7. I use this by exemple, its really simple:

  8. Andrew says:

    Appreciated – Was just about to try and find something like this!

  9. Adam S says:

    Yeah, I’d suggest you use placeholder, and you can mod the script to pull the “placeholder” value for pre-HTML5 browsers. You can detect placeholder support:


    function supports_input_placeholder() {
    var i = document.createElement('input');
    return 'placeholder' in i;
    }

  10. html5 placeholder it will be easier…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

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

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.