Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am well aware of requireJS feature of magento 2. I used it quite well for some JS scripts.

require(['jquery', 'ajaxform', ''], function (jQuery)
{
         // JS code
});

But now i have a template HTML for which i have custom JS files to perform actions.

Like -

<div class="font-dp">
     <div class="">
          <select class="form-control" id="font-dropdown">
          </select>
       </div>
</div>

Now here i am fetching options for above select tag dynamically using JSON from a custom JS file. I have not used any JS code on template file. I have called events on each HTML element from custom JS files. I have nearly 15-20 JS files like this.

So i am confused about how to include these JS files in my custom module. I have rendered my template on front-end using custom module. How should I add JS files?

share|improve this question
1  
Can you use app\code\Custom\Module\view\frontend\layout\XXXXXXX.xml <head></head>? – Ankit Shah Sep 6 at 9:32
    
How would it work? It is not requireJS way, is it? – HungryDB Sep 6 at 9:41
    
Do you want to include your JS files in your Custom Module right? You need requireJS way ok got it – Ankit Shah Sep 6 at 9:45
1  

I found a solution at last. Thanks @mike-smith for reference.

I added something like this in Namespace/Modulename/view/frontend/modulename_index_index.xml

 <head>
    <!--For CSS-->
    <css src="Namespace_Modulename/css/my_custom.css"/>

    <!--and for JS-->
    <link src="Namespace_Modulename/js/my_custom.js"/>
</head>

I added CSS file my_custom.css in Namespace/Modulename/view/frontend/web/css/ folder and JS file my_custom.js in Namespace/Modulename/view/frontend/web/js/ folder.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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