Ygo's world About games, gaming and game development and coding in general


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 🙂

Posted by ygo

Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

Trackbacks are disabled.