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 You must have JavaScript enabled to use this form. Your name Subject Comment About text formats Text format Plain textBasic_html Plain text No HTML tags allowed. Lines and paragraphs break automatically. Web page addresses and email addresses turn into links automatically.
Comments