[Solved] Explain what the function does [closed]


It hooks the submit event on all forms that exist as of when that code runs and, when the event occurs, prevents the default action (submitting the form) and instead dumps out the form contents to the web console.

Details:

//  v--- 1     vvv--2        v---- 3
    $( "form" ).on("submit", function( event ) {
//     ^^^^^^--4   ^^^^^^^^--5         ^--- 6
      event.preventDefault();
//          ^---7
      console.log( $( this ).serialize() );
//    ^--8         ^-- 9     ^--10
    });
  1. jQuery function, in this case looking for elements matching the selector string

  2. On the resulting jQuery object, call the on function to hook up an event handler

  3. The function to call when the event occurs

  4. The selector, in this case saying to find all form elements that exist on the page at the time this code runs

  5. The event to hook: form submission

  6. Argument to the handler: The event that occurred (an object)

  7. Tells the browser not to perform the default action for this event (which would be submitting the form)

  8. A debugging function that outputs to the web console

  9. That same jQuery function as in #1, but this time instead of looking for matching elements, it just wraps a jQuery object around the object in this (which refers to the form on which the event occurred)

  10. jQuery’s serialize function creates a string from the form’s field names and values

Suggested reading: http://api.jquery.com.

solved Explain what the function does [closed]