Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery delete text onfocus

Tags:

jquery

text

I've been trying to have a default value in a text input, then with jQuery, when you "onfocus" the textbox, it makes the text black (default a light gray), and deletes the default value. I think you know what I'm talking about, so do you know how to accomplish this?

like image 405
phillip usa Avatar asked Mar 27 '10 17:03

phillip usa


People also ask

How remove textbox value after submit in jQuery?

find('input:text'). val(''); $('input:checkbox'). removeAttr('checked'); }); One will clear all text inputs.

How do you remove input field focus?

The blur() method removes focus from an element.


2 Answers

<input class="txtClass" type="text" defaultVal="Enter your Name" />
<input class="txtClass" type="text" defaultVal="Enter your Designation" />

javascript

$('body').ready(function(){
  $('.txtClass').each( function () {
    $(this).val($(this).attr('defaultVal'));
    $(this).css({color:'grey'});
      });

  $('.txtClass').focus(function(){
    if ( $(this).val() == $(this).attr('defaultVal') ){
      $(this).val('');
      $(this).css({color:'black'});
    }
    });
  $('.txtClass').blur(function(){
    if ( $(this).val() == '' ){
      $(this).val($(this).attr('defaultVal'));
      $(this).css({color:'grey'});
    }
    });
});

See working example: http://jsbin.com/ovida3
http://jsbin.com/ovida3/2

like image 119
Rakesh Juyal Avatar answered Oct 14 '22 13:10

Rakesh Juyal


The jquery watermark plugin allows you to achieve the desired effect.

like image 24
Darin Dimitrov Avatar answered Oct 14 '22 12:10

Darin Dimitrov