Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.