views:

36

answers:

2

For my jq driven form I am looking for a function that clears my form and set it back to the original stage as it was on page load but without refreshing the entire page. I use placeholder text and I have created a standard reset button. In webkit browsers it works fine. If the reset button is hit, the form returns to the original state, showing the placeholder text BUT in IE (currently using 8) the form get cleared but the placeholder text is not comming back. Any idea how I can clear the form but getting back my placeholder text (I am using the placeholder.js plugin for that)? I tried the following but it doesn´t work:

$("#refresh").click(function() {
    $('#name, #email, #betreff, #nachricht').val('');
});

Any help out there?

EDIT
Link to the placeholder plugin:http://github.com/mathiasbynens/Placeholder-jQuery-Plugin

...and yes, I am talking about foing it now with the help of jquery because it can´t be done with a standard html reset button. I mean it can but not in IE. IE clears the form and does not show the placeholder text again. Webkit browser are working fine with that standard reset button, showing the placeholder text again. Now I am looking for a solution to have it the same way in all browsers.

A: 
$("#refresh").click(function() {
    $('#name').val($('#name').attr('placeholder'));
    $('#email').val($('#email').attr('placeholder'));
    $('#betreff').val($('#betreff').attr('placeholder'));
    $('#nachricht').val($('#nachricht').attr('placeholder'));
});

Is this working ? (I can't test from here) If it works, you may want to use shortcuts with input and some selectors.

Chouchenos
@Chouchenos - Close but not perfect. It only brings back the placeholder text of the field that I have typed in some text before I clicked the reset button. The placeholder text of all other fields that I did not click into before disappears. Do I make myself understandable?
Hardy
+1  A: 

Make the id="refresh" just a standard button as to not clear the entire form. You will have to manually "reset" the form elements that use the placeholder via the function below:

$("#refresh").click(function() {
    $('#name, #email, #betreff, #nachricht').val('').placeholder();
});

You will have to clear the non-placeholder elements using val('');

You can also try a more general approach which is to select all related form elements with the placholder attribute (assuming you did so in your HTML or before this script runs):

$("#refresh").click(function() {
    $("input[placeholder],textarea[placeholder]").val('').placeholder();
});

This should replace all the input/textarea elements so any other elements you will have to add to the selector.

methodin
@Methodin - Bingo, that seems to work for me. Will do some more testing but it looks very good. Thank you - makes me feel very happy on that work loaded day. Cheers
Hardy
I've updated it in case you want to reset ALL the placeholder elements without listing the ids. Hope it helps.
methodin
@Methodin - I tried this but actually it was not clearing any field at all. Something missing....?
Hardy
I realized I missed the .val('').placeholder(). Give that a shot.
methodin
$("input[placeholder],textarea[placeholder]").placeholder().val(").placeholder(); - You mean like this?
Hardy
I think I am messing it up....I better leave it with your first solution. You are think about radio and checkbox items, right? I just think I am not getting your second solution right.
Hardy
@Methoding - Could you please complete with the final code you had in mind because I would like to accept your answer but the code as it is right now is not working. I would appreciate that final change a lot because your aproach was helping me a lot.
Hardy
Done. Edited to the original solution plus a more detailed and hopefully fixed version of the generalized approach I was going for.
methodin
@Methodin - Cool, thank you. I now have accepted your answer. Works wonderful.
Hardy