How to set default text if input text is empty?

how can i do this using jquery? I am using the change event, but if I submit or reset the form, the text will not be set again. I need to show the text (this is a datapicker, so I need to show the date) inside the input.

How to do it?

Thanks!

+4
source share
5 answers

You can do it without Javascript. Using HTML5 is easier:

<input type="text" placeholder="default text" /> 
+2
source

This is how I usually do it.

 <form id="myform"> <input name="box" type="text" value="Type here"> </form> 

_

 $("input[name='box']", "#myform").bind("blur focus", function() { if ($(this).val() == "" ) { $(this).val("Type here"); }else if ($(this).val() == "Type here" ) { $(this).val(""); } }); 

And here is a fiddle to show how this works: http://jsfiddle.net/cR6P8/1/

If necessary, it can be associated with a download event.

+1
source
 $('#textbox').blur(function() { if( $('#textbox').val() == "" ) { //show the date } } 
-1
source

Actually your question is not very clear, but

 var yourVaribale; $('yourTextSelector').val(yourVariable) 

u can set the input text value as above

-1
source

you can do something like

 $(document).ready(function(){ if ($("input[id$='your textbox id']").val().length == 0 || $("input[id$='your textbox id']").val().trim() == " ") { $("input[id$='your textbox id']").val("something"); }); 
-1
source

All Articles