3

How can i use custom ajax command using #ajax api , i have the following code working well

        $ajax_response = new AjaxResponse();                
        $ajax_response->addCommand(new InvokeCommand('#edit-event-trigger', 'attr', array('disabled',true)));
        $ajax_response->addCommand(new InvokeCommand('#edit-event-trigger', 'removeAttr', array('placeholder')));
        $ajax_response->addCommand(new InvokeCommand('#edit-event-trigger', 'css',array("background-color", "")));
        //Return the AjaxResponse Object.
        return $ajax_response;

But suppose if i want to focus() or hide() the edit-event-trigger element , how could i do that ?

7
  • 1
    did you try 'focus' and 'hide'?
    – 4uk4
    Aug 16, 2016 at 13:18
  • tried like this : $ajax_response->addCommand(new InvokeCommand('#edit-event-trigger', 'focus'));
    – KTM
    Aug 16, 2016 at 13:35
  • This is ok, did you check for php errors, could be a missing use statement?
    – 4uk4
    Aug 16, 2016 at 15:42
  • yes i checked , am using the following ajax classes : use Drupal\Core\Ajax\ChangedCommand; use Drupal\Core\Ajax\CssCommand; use Drupal\Core\Ajax\HtmlCommand; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\InvokeCommand;
    – KTM
    Aug 16, 2016 at 15:43
  • 1
    I think there is nothing wrong with your ajax commands. They should work.
    – 4uk4
    Aug 16, 2016 at 15:52

1 Answer 1

5

These are some examples you can try:

$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand('.message', 'addClass', array('show')));
$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand('.message', 'removeClass', array('show')));
$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand('.message', 'attr', array('value', '123')));
$response->addCommand(new \Drupal\Core\Ajax\HtmlCommand('.message', "Show this text"));
$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand('.message', 'css', array('background', '#dff0d8')));

Peace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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