Using jQuery in a Magento 2 static block or CMS page can be tricky, a special syntax is required. Use the following code to add jQuery when all the content was loaded: Of course, you can skip listening to domReady…
Using jQuery with domReady in a CMS page or static block (Magento 2)
Want to use jQuery in a static block or CMS page? No problem! Open the page or block in Magento 2 admin and insert the following code:
|
1 2 3 4 5 6 7 8 |
<script> require([ 'jquery' ,'domReady!' ], function ($) { console.log('whatever'); }); </script> |
If you do not want to wait for the domReady event, simply…
Using jQuery in a template (phtml) (Magento 2)
The following code shows how to load and use jQuery in a Magento 2 .phtml template.
|
1 2 3 4 5 6 7 8 9 |
<script> // Separate additional libraries with commas // In this example, jQuery UI require(['jquery', 'jquery/ui'], function($){ $(document).ready( function() { alert("fertig :-)"); }); }); </script> |