The form examples show how to add graceful degradation so AJAX behavior can work when JavaScript is disabled. This is done with a submission button that's hidden with a CSS class.

    // Since we don't know if the user has js or not, we always need to output
    // this element, then hide it with with css if javascript is enabled.
    $form['instrument_family_fieldset']['choose_family'] = [
      '#type' => 'submit',
      '#value' => $this->t('Choose'),
      '#attributes' => ['class' => ['ajax-example-hide', 'ajax-example-inline']],
    ];

This is using a custom CSS class.

However, Drupal core already provides a CSS class for the same purpose.

/**
 * For anything you want to hide on page load when JS is enabled, so
 * that you can use the JS to control visibility and avoid flicker.
 */
.js .js-hide {
  display: none;
}

The example should use the CSS class already defined from Drupal core, instead of using a new CSS class.

Issue fork examples-3350455

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

joachim created an issue. See original summary.

avpaderno’s picture

Component: Form API Example » AJAX Example
avpaderno’s picture

Title: Use the system module CSS classes to hide form elements when JS is present » Use the Drupal core CSS class to hide form elements when JavaScript is enabled
Category: Bug report » Task
Issue summary: View changes

avpaderno’s picture

Status: Active » Needs review

  • apaderno committed 4ac80183 on 4.0.x
    Issue #3350455: Use the Drupal core CSS class to hide form elements when...
avpaderno’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.