Default text in input field

I donโ€™t know if this has a special name for it, but is there an easy way to set the default text in the input field, which disappears in focus and appears again when blurred, if the text field is empty?

+6
javascript jquery jquery-ui
source share
7 answers

the word WATERMARK

I recommend using the plugin if you do not have time to develop and test. There are many problems and side effects to consider when writing this code.

  • Confirm the field as "blank" if it contains watermark text. Thus, select the watermark text from the entered text.
  • Do not send watermark text.
  • Prevent the user from entering the watermark text itself :)
  • Styling watermark text.
  • etc. etc.
+4
source share

You can use the new HTML5 placeholder attribute.

Edit : update to use another hot HTML5 / jQuery, HTML5 data storage.

<input type="text" placeholder="type here" data-placeholder-text="type here" /> 

This will work in all modern browsers. And gracefully degrades in IE. However, for IE you will have to use javascript.

 $(document).ready(function() { var $input = $('#id_of_input_element'); $input.focus(function() { if($(this).val() == $(this).data('placeholder-text')) { $(this).val('') } }).blur(function() { if($(this).val() == '') { $(this).val($(this).data('placeholder-text')); } }).trigger('blur'); }): 
+19
source share

Functionality is known as a watermark and can be obtained in many ways, one of which

 onfocus="if(this.value=='Enter Email')this.value=''" onblur="if(this.value=='')this.value='Enter Email'" 

This will work for an email text field.

+4
source share

Here is the solution .

 $(".defaultText").focus(function(srcc) { if ($(this).val() == $(this)[0].title) { $(this).removeClass("defaultTextActive"); $(this).val(""); } }); $(".defaultText").blur(function() { if ($(this).val() == "") { $(this).addClass("defaultTextActive"); $(this).val($(this)[0].title); } }); $(".defaultText").blur(); 
+2
source share

you can try: using the title attribute to keep the default text

 $(function(){ $(".makedefault").bind("blur",function(){ if($(this).val().length == 0) { $(this).val($(this).attr('title')); } }); $(".makedefault").bind("focus",function(){ if($(this).val() == $(this).attr('title')) { $(this).val(""); } }); 

});

+2
source share

You can use this plugin (I am a co-author)

https://github.com/tanin47/jquery.default_text

He clones the input field and puts it there.

It works on IE, Firefox, Chrome, and even iPhone Safari, which has a known focus problem.

This way you don't have to worry about clearing the input field before submitting.

OR

If you want only HTML5, you can simply use the "placeholder" attribute in the input field

+1
source share

Here is my approach (with jQuery):

 <input id="myBox" type="text" /> <script> // Input field and default text var $element = $('#myBox'); var defaultText = 'Hello World'; $element.focus(function(){ // Focused $element.removeClass('defocused').addClass('focused'); if($element.val() == defaultText) $element.val(''); }).blur(function(){ // Defocused $element.removeClass('focused').addClass('defocused'); if($element.val() == '') $element.val(defaultText); }); // Initialization $element.blur(); </script> 
0
source share

All Articles