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?
>Solution :
Inside the done callback, you can target the text area element and clear it.
textarea.value = '';
$("#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);
$('#new_post__add textarea').val(""); //this is assuming there is only one textarea inside your form
});
});