Can't seem to find a similar question, so here goes; I'm writing a module that manipulates commenting on content types; basically, I'm AJAXifying the comments form so it incorprates new comments, new replies and edits (and before anyone asks, I've looked at ajax_comments and it doesn't do what I want).

Basically, on clicking the "Add" comment, I need to be able to set a class on the link element (i.e. "has-form"), and using an ajax callback within the form function thus:

$items['mycomments/newcomment/%node'] = array(
  'delivery callback' => 'ajax_deliver',
  'page callback' => 'mycomments_newcomment',
  'page arguments' => array(2),
  'access callback' => 'node_access',
  'access arguments' => array('view', 2),
  'type' => MENU_CALLBACK,

Within the callback function, I need to check whether an element ("#mycommentform") has the class of "has-form" in order to execute ajax_command_* invokes. I know in jQuery I can simply do $("#mycommentform").hasClass("has-form") but how can I do this from within the callback PHP function code?


  • 2
    You'll need to write a jQuery plugin which provides the functionality you require, then use $commands[] = ajax_command_invoke('#selector', 'pluginName'); in your AJAX callback. Drupal's AJAX framework can't execute arbitrary JS code in any other context as far as I know – Clive Aug 2 '13 at 10:49
  • Can't you use ['#ajax'] element on "Add" button and add class in callback function? As I understood you problem is in adding css class, right? – milkovsky Aug 2 '13 at 11:01
  • Thanks for your responses; what I'm stuck with is doing the conditional check whether the element has the class (i.e. an if statement to check if the element has the class name)... hope that makes sense. – WalJtD Aug 2 '13 at 11:04
  • @Clive: when you say 'pluginName', would this also include a specific function name written within an already included .js file, and arguments passed to it for instance: ajax_command_invoke('#selector', 'functionName', array(args)) ? – WalJtD Aug 2 '13 at 11:12
  • Not a 'standard' function, a custom jquery plugin with that name. Drupal will do this with your code: $('#selector').pluginName();, so for that example you'd have a plugin named pluginName – Clive Aug 2 '13 at 11:14

Your Answer

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

Browse other questions tagged or ask your own question.