What is the difference of these, and how do I know when to use which?
$.post($(this).attr('action'), $(this).serialize(), function(response) {
// do something here on success
}, 'json');
$.post($(this).prop('action'), $(this).serialize(), function(response) {
// do something here on success
}, 'json');
$.post($(this).closest("form").prop('action'), $(this).serialize(), function(response) {
// do something here on success
}, 'json');
The first and second are identical, in this case. These functions has to be called from a form event handler. Instead of
$(this).prop/attr(),$(this)[0].actionandthis.actioncan also be used.The third method looks for the nearest form element, and retrieve the
actionattribute of the form. This method would be useful from a non-form context, e.g. from abuttonelement.