Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

JavaScript Events on Drupal 8.

By editor , 13 February, 2018
Question

How to use JS event such as onclick, onload, onmouseover, onkeypress .... with Drupal behaviors ?

Note : On drupal the best practice is use drupal behaviors but not JS or Jquery codes such as:
<button onclick='myFunction()'>Click</button>


Example : Add event on click ( like onClick='Click01' )

(function ($, Drupal, window, document) {
    Drupal.behaviors.Click01 = {
        attach: function (context, settings) {
            $('#my-button-01').click(function () {
                console.log(this.id);
            });
        }
    };
}(jQuery, Drupal, this, this.document));

See the tutorial : Add JavaScript to a module.

Tags

  • Drupal 8
  • Code
  • JavaScript
  • Module
Add JavaScript to a module
Drupal Behaviors - JavaScript and jQuery
JavaScript useful tips and examples

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer