I have this app.js for my form from teamtreehouse blog I really liked it but i need tto use it for multiply forms including and a modal. I know that I should use $(this) but don't know where...
$(function() {
// Get the form.
var form = $('.ajax-contact');
// Get the messages div.
var formMessages = $('.form-messages');
// Set up an event listener for the contact form.
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('text-error');
$(formMessages).addClass('text-success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#phone').val('');
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('text-success');
$(formMessages).addClass('text-error');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Возникла ошибка и телефон не удалось отправить!');
}
});
});
});