jQuery remove text in input on focus

jQuery code:


$(function(){

    var input = $('input.text'); // input text field

    var default_value = input.val();

    input.focus(function(){

        if ($.trim($(input).val()) == default_value) input.val('');

    });

    input.blur(function(){

        if ($.trim($(input).val()) == '') input.val(default_value);

    });

});​

Or use placeholder feature:


<form class="well">

    <div class="control-group">

        <input type="text" placeholder="placeholder text" />

    </div>

    <div class="control-group">

        <textarea placeholder="placeholder text (will disappear after typing)" cols="30" rows="5"></textarea>

    </div>

</form>​

2 thoughts on “jQuery remove text in input on focus”

Leave a Comment