0

I have a Drupal 9 page, where I am trying to load some inline jQuery code, from CKEditor, but it isn't working and I am not getting any error on the console.

This worked perfectly in Drupal 8.

<script>
 window.onload = function() {
  jQuery('.className').fadeOut();
  console.log("HI");
 };
</script>
2
  • 3
    These things are generally not done, nor advised, from WYSIWYGs.
    – Kevin
    Commented Nov 6, 2022 at 16:12
  • From a frontend developer view (I do backend as well,) I think that it involves too many steps, to create modules or libraries, just to swap a couple of divs around and add some css. It is very convenient to just go into the CKEditor and splash out some jQuery, that is only meant to do its work, on a specific page or block.
    – KVF-IT
    Commented Nov 8, 2022 at 8:58

1 Answer 1

0

The right way of doing it, is to add a js file to the js folder, then declare it in the my-theme.libraries.yml file, either globally or not. Then the trick is to make a hook for the specific node (or node-type, even theme) in the my-theme.theme file.

1
  • Yeah and never put it in a contrib theme. Either create a new custom sub-theme or a custom module from where you attach the JS like it's written in the docs.
    – leymannx
    Commented Nov 7, 2022 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.