Adding the submit Handler

The next step is for FormHandler to listen for the submit event on the <form> element and run a callback when it occurs.

To make the FormHandler module more reusable, you will not hardcode the submit handler code. You will instead write a method that accepts a function argument, adds the submit listener, and then calls the function argument inside that listener.

First, add a prototype method called addSubmitHandler to formhandler.js.

...
    if (this.$formElement.length === 0) {
      throw new Error('Could not find element with selector: ' + selector);
    }
  }

  FormHandler.prototype.addSubmitHandler = function () {
    console.log('Setting submit handler for form');
    // More code will go here
  }; App.FormHandler = FormHandler; ...

Get Front-End Web Development: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.