Posted By

andres_314 on 10/07/10


javascript input Value

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Text input default value recover

 / Published in: jQuery


This snippet could be used when a text input has a default value. Thus, when user clicks on the input to write, the default value disappears and allows the user to write, but if the user does not type anything, the default value will appears again. It also adds a class to the input when the user is typing on it and is removed when the input loses focus.

This snippet must be inside the ready event.

For the HTML markup, the only thing to take care is that you must use the same class in all the inputs that you want to give this functionality. In the example I use \"input-text-js\", but you can use whatever you want.

You can see this snippet working on

  1. var textBoxs = $('.input-text-js');
  2. textBoxs.each(function() {
  3. var theValue = $(this).attr('value');
  4. $(this).focus(function (){
  5. if($(this).attr('value') == theValue){
  6. $(this).attr('value','').addClass('writingIt');
  7. }
  8. }).blur(function () {
  9. if($(this).attr('value') == ''){
  10. $(this).attr('value',theValue).removeClass('writingIt');
  11. }
  12. });
  13. });

Report this snippet  

You need to login to post a comment.