5

how can i do it with JQuery? I use the change event but if i submit or reset the form the text is not set again. I need to show a text(it is a datapicker, so i need to show a date) inside the input.

How to do it?

Thanks!

5 Answers 5

3

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

<input type="text" placeholder="default text" />
Sign up to request clarification or add additional context in comments.

2 Comments

I need to do it with javascript, because after resetting the form is the day change javascript need to calculate the date again, it is an "ajax" application
I don't know jQuery, so I will write this in Prototype: $('idOfInputTextBox').writeAttribute('placeholder', theChangedDateString);
1

Here's 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's a fiddle to show how it works: http://jsfiddle.net/cR6P8/1/

If necessary it could probably be bound to the load event as well.

1 Comment

Did you? A reset button works just fine, have a look : jsfiddle.net/cR6P8/3 - And who would want the form fields to go to default when the browser back button is clicked. Chances are that if someone is submitting the form and then clickicng the browser back button, they typed something wrong, and would not like to type everything in one more time, but to just change what they already typed in. This is one of those default browser actions that shouldn't be fidddled with, but if really necesary I suppose binding to browser history would be possible somehow.
-1
$('#textbox').blur(function() {

if( $('#textbox').val() == "" ) {
    //show the date
}


}

2 Comments

after reset the "blur" event is not called.
if you want to check it after a form submit you can just do the check in document ready
-1

Actually your question is not very clear but ,,

var yourVaribale;

$('yourTextSelector').val(yourVariable)

u can set a value of an input text like above

1 Comment

I need to set that text IF the text is empty, so at the beginning...ok, but fi the user delete it or send/reset the form i need to show it again..unfortunately the blur event is not called after the resetting of the form.
-1

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");
});

2 Comments

document.ready will be called every time page loads so even if the user delete it or send/reset the form this should work
well document.ready is not the main part main part is replacing the text you can add the same while resetting the form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.