30Jan/130
Clear and restore default text in input with jQuery
Today I wanted to put some text in a input for user, so he know what values should type in. When you do that the problem is if user let the field blank you have to reload the text again. The most common is the username input where you want to have "Username" text.
After many iteration of the javascript code (going back to the old days when there was no fancy library) I refined my code to this:
var tempVal = "";
$(".someclass").focus(function(){tempVal = $(this).val();$(this).val("");}).blur(function(){if($(this).val()=="") {$(this).val(tempVal);}});
That's a pretty neat piece of code 🙂
Leave a comment
You must be logged in to post a comment.