I’m using PHP and Ajax to post without page refresh. Everything works fine but the textarea content is not reset after submitting the form.
This is my Ajax code:
$("#new_post__add").submit(function(event){
event.preventDefault();
var post_url = $(this).attr("action");
var request_method = $(this).attr("method");
var form_data = $(this).serialize();
$.ajax({
url : post_url,
type: request_method,
data : form_data
}).done(function(response){ //
$("#load_data").html(response);
});
});
How can I reset the textarea value after successfully submitting the form? can you please edit my code?
3
Answers
Inside the done callback, you can target the text area element and clear it.
textarea.value = '';
Try using
success
instead ofdone
. Depending on the version,success
may or may not be deprecated.If it still doesn’t work, I’d add
fail
andalways
handlers withconsole.log
output to see what’s happening. If you’re suresuccess
is not deprecated in your version, then the equivalent areerror
andcomplete
. Or maybe I’ve mixed versions. Hell, you can have all six, and see which get called!You can also reset all elements within the form by doing any of the following