magentomagento2magento-1.8

Add jquery to Mganto pages inside admin panels [wysiwig editor]


How can I add jQuery code inside magento2 pages? For example I have a "Contact us" page. So I can edit the content inside contact us page by

Login to Admin panel

enter image description here

admin -> content -> Pages -> Contact us

I want to add some jQuery code.

So I write

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
  <script>
   jQuery(function($){
    $(".mybox").on("click", function(){
             MY CODE *******
       });
 });
</script>

Here the problem is: I have to call <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">

Otherwise it will not work. How can I solve this?

ie. How can i add jquery code in wysiwig editor


Solution

  • Magento 2 Uses Require js

    Try using

    <script>
    require([
        'jquery'
    
    ], function ($) {
        $(".mybox").on("click", function () {
            console.log('your code starts here');
    
            console.log('your code ends here');
        });
    });
    </script>