0

I have a button with ajax callback and I want to disable it on button click without page reload. Preferably inside ajax_function but it's not obligatory.

$form['my_button'] = [
  '#type' => 'button',
  '#ajax' => [
    'callback' => '::ajax_function',
    'wrapper' => 'my-button-wrapper',
  ],
  '#attributes' => array(
    'class' => array('glyphicon', 'glyphicon-shopping-cart'),
  ),
  '#submit' => ['::ajaxButtonSubmit'],
];
  • I did this with a Javascript behavior in Drupal 7 disabling an overlay and also adding an overlay on the page to prevent clicks. It did not have a non-JavaScript fallback. – mradcliffe Aug 28 at 11:37
0

In your ajax callback function write the code to disable button. like:

public function ajax_function(array &$form, FormStateInterface $form_state) {
    $form['my_button']['#attributes']['disabled'] = true;
    return $form['my_button'];
}

Hope it helps!

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.