Last updated February 5, 2020

Anyone writing JavaScript for Drupal should use the Drupal.behaviors API when writing their custom JavaScript functionality. Doing so ensures that your JavaScript is executed at the appropriate times during the life cycle of a page, such as when the page loads, or after new DOM elements have been added via an AJAX request. In this tutorial we'll look at the problem that Drupal.behaviors solves and how to use Drupal.behaviors when writing your JavaScript code. By the end of this tutorial you should know how, and when, to use the Drupal.behaviors API in your projects.