I have below code in my Form. But after rendering i could not see the onClick attribute in the link.

$form['step2_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Next'),
    '#prefix' => '<div class="left"><a onClick="javascript:back_callback(1);" href="javascript:void(0)" class="back_btn">'.t('Back').'</a></div><div class="right">',
    '#suffix' => '</div>',
    '#name' => 'step2',
    '#attributes' => array('class' => array('enabled-for-ajax')),
    '#ajax' => array(
                'callback' => '::myajaxmethod',

But i got the below output, without onClick attribute and javascript: in the output.

<div class="left"><a class="back_btn" href="void(0)">Back</a></div>

Is this restricted? If so, how to achieve this in drupal 8?


Try this code: $form['cancel'] = array( '#markup' => "<div class="left"><a onClick="javascript:back_callback(1);" href="javascript:void(0)" class="back_btn">'.t('Back').'</a></div>" ); $form['step2_submit'] = array( '#type' => 'submit', '#value' => t('Next'), '#prefix' => '<div class="right">', '#suffix' => '</div>', '#name' => 'step2', '#attributes' => array('class' => array('enabled-for-ajax')), '#ajax' => array( 'callback' => '::myajaxmethod', ) );

|improve this answer|||||
  • Thanks. But, still my question is, placing a link (a tag) in '#prefix' with onclick attribute is not the right way of implementation? – siddiq Feb 19 '18 at 5:35
  • @siddiq Yes, It is not a right way to implement any link. – Jeetendra Kumar Feb 19 '18 at 6:09

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.