jQuery: Clear / Return Textbox Value

June 10, 2013 10:15 am Published by Leave your thoughts

Here’s a little jquery file that clears out a textbox’s value onFocus and returns it onBlur if it did not change.
‘Tis really handy for developing for mobile devices.

All you need to do is give the textbox a class of “tbxreq” if you don’t want the value to return at all use the class “tbx
e.g.

<input id="tbx_username" type="text" name="username" value="Username" class="tbxreq" />
<input id="tbx_password" type="text" name="password" value="Password" class="tbx" />
$(function () {
    $('.tbxreq').focus(function () {
        var orgval = $(this).val();
        $(this).val('');
        $(this).blur(function () {
            if ($(this).val().trim() == '') {
                $(this).val(orgval);
            } else {
                orgval = $(this).val();
            }
        });
    });

    $('.tbx').focus(function () {
        $(this).val('');
    });
});
Tags: , ,

Categorised in:

This post was written by admin

Leave a Reply

Your email address will not be published. Required fields are marked *