Is it possible for a Drupal Ajax form to automatically fire a callback when a page is loaded?

I know this is possible using an external JS file and doing it through Drupal behaviors, however I would prefer to be able to do is straight from the Drupal form callback as this allows for less code and more security.

I am trying to do something like this:

$form['mark'] = ['#markup' => '<div id="mytestid">REPLACE ME</div>'];
$form['test'] = [
  '#type' => 'textfield',
  '#title' => 'My Title',
  '#ajax' => [
    'callback' => '::callback',
    'event' => 'load',

The load event doesn't work, which makes me think Drupal doesn't want to allow this type of functionality. When I use something like click or mouseout it does work. Is there some type of event here that I can use that will fire this ajax callback when the form loads? Or is it not possible this way?


1 Answer 1


The load event in jQuery doesn't work with elements without URL. This is not a Drupal issue. You can't use it this way.

  • Thank you for the response. Is there a way to do some type of page load event through the ajax form or am I going to need to do this through Drupal behaviors? Sep 12, 2018 at 20:28
  • Drupal behaviors is the way to go since they are fired each time an Ajax call is made and on page load Sep 12, 2018 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.