Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Textarea value not resetting in post action with Ajax

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
    });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading