Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent Drupal from focusing on element that trigger ajax submit?

When ever I submit a form using ajax, the element that triggered the submit gets focus. I don't want this behavior if I'm using infinite scroll and I want to keep the user at the bottom of the page. How do I fix this?

Focus returns to this element:

     $form['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Search'),
        '#attributes' => [
            'class' => [
                'btn',
                'btn-md',
                'btn-primary',
                'use-ajax-submit'
            ]
        ],
        '#ajax' => [
            'wrapper' => $wrapper,
        ]
    ];
like image 445
Je Suis Alrick Avatar asked Jan 21 '17 08:01

Je Suis Alrick


1 Answers

To solve this problem I added the following changes:

    $form['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Search'),
        '#attributes' => [
            'class' => [
                'btn',
                'btn-md',
                'btn-primary',
                'use-ajax-submit'
            ]
        ],
        '#ajax' => [
            'wrapper' => $wrapper,
            'disable-refocus' => true
        ]
    ];

The disable-refocus flag prevents the automatic refocus.

See: Focus state bug on text field AJAX calls

like image 88
Je Suis Alrick Avatar answered Nov 15 '22 01:11

Je Suis Alrick