everyone. I have a (set of) form(s) with a textarea inside. This textarea has property required
. It works as expected. If the user tries to submit the form without filling the textarea, the browser hightlights it and notify the user.
My problem is that I intercepted form submitting with JQuery AJAX, so I can notify the user without reloading the page. The code is the following:
$(function () {
$('form').each(function (id, el) {
$(el).submit(function (event) {
event.preventDefault();
var _data = $(el).serialize();
alert(_data);
$.ajax({
type: 'POST',
url: $(el).attr('action'),
data: _data
})
.done(function(response) {
$("#notify-modal").find('.modal-body').removeClass('alert-danger');
$("#notify-modal").find('.modal-body').addClass('alert-success');
$("#notify-modal").find('.modal-body').text('Já recebemos sua história! Obrigado por compartilhá-la conosco!');
$("#notify-modal").modal('show');
$(el).find('textarea').val('');
})
.fail(function(data) {
$("#notify-modal").find('.modal-body').removeClass('alert-success');
$("#notify-modal").find('.modal-body').addClass('alert-danger');
$("#notify-modal").find('.modal-body').text("Erro ao enviar história! Contate os administradores do site! " + data.responseText);
$("#notify-modal").modal('show');
});
});
});
});
When the user submits a valid information, the AJAX function works as expected posting to my PHP script and displaying the modal that informs the user. But note that in .done()
function there's a statement that clears the textarea after the data is submited ($(el).find('textarea').val('');
). My issue is that when I clear the textarea it gets highlighted, even if the data was properly submitted, as if had been cleared before submiting. How can I prevent this behaviour?