0

I have written multiple jquery function in my footer for carousel,hiding toggle bar menu on click etcc.But instead of adding in footer i need to create one custom.js file and paste all this code and call that js file. I have tried in this way but it didn't work.

<script type="text/javascript">
    $(function () {
        $('.nav a').filter(function () { return this.href == location.href }).parent().addClass('active').siblings().removeClass('active')
        $('.nav a').click(function () {
            $(this).parent().addClass('active').siblings().removeClass('active')
        })
    })

        (function () {
            $('#carousel123').carousel({ interval: 2000 });
            $('#carouselABC').carousel({ interval: 3600 });
        }());

    (function () {
        $('.carousel-showmanymoveone .item').each(function () {
            var itemToClone = $(this);

            for (var i = 1; i < 4; i++) {
                itemToClone = itemToClone.next();

                // wrap around if at end of item collection
                if (!itemToClone.length) {
                    itemToClone = $(this).siblings(':first');
                }

                // grab item, clone, add marker class, add to collection
                itemToClone.children(':first-child').clone()
                    .addClass("cloneditem-" + (i))
                    .appendTo($(this));
            }
        });
    }());

    $(function () {
        $('#ChangeToggle').click(function () {
            $('#navbar-hamburger').toggleClass('hidden');
            $('#navbar-close').toggleClass('hidden');
        });
    });

    $(document).on('click', function () {
        $('.collapse').collapse('hide');
        $('#navbar-hamburger').toggleClass('show');
        $('#navbar-close').toggleClass('hidden');
    })
</script>
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
tester
  • 429
  • 5
  • 20
  • possible duplicate of https://stackoverflow.com/questions/13739568/how-do-i-link-a-javascript-file-to-a-html-file – Matt.S Jan 08 '19 at 08:15
  • @Matt.S the question which you have posted was How do I link a JavaScript file to a HTML file? but my question how to write all the jquery function in a single file like keeping as custom.js file i know how to link the file but how to add multiple functions into a single file – tester Jan 08 '19 at 08:17
  • yeah thats how you do it... you can write as many functions as you want in the .js file – Matt.S Jan 08 '19 at 08:20

2 Answers2

2

Create custom.js file and past your code in this file, remove <script> from custom.js in custom.js no need to enclose your script with <script type="text/javascript"></script> so remove this.

Include this file from your main page <script src="YOUR_PATH/custom.js"></script>

AJAY MAURYA
  • 541
  • 3
  • 11
0

Check out the module pattern. Basically you have to put all of these functions in an object or a self invoking function, and put that in your custom file. Import that file, you will have these functions available in the source that uses it. Although you might have to make sure the references for the elements are available there.

https://coryrylan.com/blog/javascript-module-pattern-basics

Rutwick Gangurde
  • 4,772
  • 11
  • 53
  • 87