JavaScript AJAX Basics (retiring) jQuery and AJAX Posting Data with jQuery

Nicholas Wallen
Nicholas Wallen
12,278 Points

Does serialize automatically look for form data?

He explicitly pointed to the action part in regards to where the URL goes to, but the part that is actually supposed to point to the form itself....where is that?

1 Answer

Hi Nicholas,

The form is selected using jQuery like so: $('form')

Then, an event handler is added to the form for the submit event: $('form').submit()

Finally, the submit function receives a callback function that does the processing when the submit event occurs. Therefore, all of the code taking place within the event listener's callback function can reference the form itself using the 'this' keyword.

So this line here: var formData = $(this).serialize(); Calls the jQuery serialize function on the form itself.

If you are curious to know more about how serialize() is able to pull the data from the individual form elements, the jQuery link below has good documentation and a demo that can thoroughly explain its inner workings.