jQuery remove text in input on focus

The next code needs jQuery to be included:

jQuery(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 just use placeholder html5 feature:

<form>
	<input name="q" placeholder="search...">
</form>

Test html5 placeholder example:

VN:F [1.9.17_1161]
Rating: 0 (from 2 votes)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">