/ Published in: jQuery
input focus&blur actions
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//search .click events var default_value = new Array(); $('input[type=text], textarea').each(function(){ default_value[$(this).attr('name')] = this.value; }); $('input[type=text], textarea').focus(function(){ if(typeof(default_value[$(this).attr('name')]) != 'undefined' && this.value == default_value[$(this).attr('name')]){ this.value = ''; } }); $('input[type=text], textarea').blur(function(){ if(typeof(default_value[$(this).attr('name')]) != 'undefined' && this.value == ''){ this.value = default_value[$(this).attr('name')]; } }); // even a better option - this takes the title value // toggle input text on blur $('input.toggle-text').focus(function(){ var input = $(this); var defaultText = input.attr('title'); input.val(''); input.blur(function() { var userInput = $(this).val(); if (userInput == ''){ $(this).val(defaultText); } }); });