Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

In this post, I'll show you how to add target attribute to form and submit buttons in Drupal 8.

You have to set the target attribute in the form tag itself:

$form['#attributes']['target'] = '_blank';

To get this functionality to a particular button:

$form['save_and_print'] = array(
  '#type' => 'submit',
  '#value' => t('Save and Print'),
  '#attributes' => array('onclick' => 'this.form.target="_blank";return true;'),
);

To submit the form in same page:

$form['save'] = array(
  '#type' => 'submit',
  '#value' => t('Save'),
  '#attributes' => array('onclick' => 'this.form.target="_self";return true;'),
);

or with jquery:

$('#edit-submit').on('click', function () {
  this.form.target = "_self";
  return true;
});

To submit in a new tab:

$form['save_and_print'] = array(
  '#type' => 'submit',
  '#value' => t('Save and Print'),
  '#attributes' => array('onclick' => 'this.form.target="_blank";return true;'),
);

or with jquery:

$('#edit-print').on('click', function () {
  this.form.target = "_blank";
  return true;
});

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon