cancel
Showing results for 
Search instead for 
Did you mean: 

Add Custom JS to my custom module frontend

SOLVED

Add Custom JS to my custom module frontend

I have created my custom module in that a particular .phtml file i want to apply my custom JS

please suggest as soon as possible

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Add Custom JS to my custom module frontend

Hi @asd_asd46 

If you want to add js with phtml file then you can consider below code and add with phtml file

<script>
        require(['jquery', 'myscript'], function($, myscript) {
         cosole.log("testing");
        });
    </script>

Or you can refer below link :
https://magento.stackexchange.com/questions/301627/magento-2-how-to-include-custom-js-file-in-phtml-...

If issue resolve, please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

View solution in original post

4 REPLIES 4

Re: Add Custom JS to my custom module frontend

Hi @asd_asd46 

If you want to add js with phtml file then you can consider below code and add with phtml file

<script>
        require(['jquery', 'myscript'], function($, myscript) {
         cosole.log("testing");
        });
    </script>

Or you can refer below link :
https://magento.stackexchange.com/questions/301627/magento-2-how-to-include-custom-js-file-in-phtml-...

If issue resolve, please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

Re: Add Custom JS to my custom module frontend

Hi @asd_asd46,

 

Can you please check below link:

https://inchoo.net/magento-2/custom-javascript-in-magento-2-with-requirejs/

 

Hope this helps you.

Problem Solved! Click kudos & Accept  as Solution!

Re: Add Custom JS to my custom module frontend

Thank you for valuable suggestion.Give sample code is working in my .phtml file

 

But i have 2 files

1) Jacker_scroller.js and 2) jquery.pagepepling.js

i want to include these both files in .phtml please suggest on this

Re: Add Custom JS to my custom module frontend

Hi @asd_asd46 ,

 

Can you please try below codebase in your phtml file

 

require(['jquery', 'VendorName_ModuleName/js/Jacker_scroller', 'VendorName_ModuleName/js/jquery.pagepepling'], function($, jackscroller, pagepepeling) {
cosole.log("testing");
});

 

Note: 1. Please replace VendorName_ModuleName with your custom module.

2. Also, try to use lower case letters in js names.

 

For more details, you can follow below link:

https://magento.stackexchange.com/questions/301627/magento-2-how-to-include-custom-js-file-in-phtml-...

 

Hope it helps you!

Problem Solved! Click Kudos & Accept as Solution!