CLEAR INPUT ON FOCUS FUNCTION


/ Published in: jQuery
Save to your folder(s)

Clears input or search field on focus.


Copy this code and paste it in your HTML
  1. $(document).ready(function() {
  2. //attach function to input and make text grayed out on page load
  3. textReplacement($('#query').css("color", "#999"));
  4. });
  5.  
  6. // the function:
  7. function textReplacement(input){ //input focus text function
  8. var originalvalue = input.val();
  9. input.focus( function(){
  10. if( $.trim(input.val()) == originalvalue ){ input.val('').css("color", "#000"); }
  11. });
  12. input.blur( function(){
  13. if( $.trim(input.val()) == '' ){ input.val(originalvalue).css("color", "#999"); }
  14. });
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.