Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import and export a jquery function

I have a custom.js file with jquery code :

$('.nav.navbar-nav > li').on('click', function(e) {
$('.nav.navbar-nav > li').removeClass('active');
$(this).addClass('active');
});

I want to export this code to the main.js file.Both files are in the same folder.

Went through the documentation but couldn't understand how to do it with jquery.

like image 743
Arkadiy Stepanov Avatar asked Apr 19 '26 18:04

Arkadiy Stepanov


1 Answers

First you need to wrap this code in a function. Then you export the function. Finally you import it in main.js:

custom.js

export function foo () {
    $('.nav.navbar-nav > li').on('click', function(e) {
        $('.nav.navbar-nav > li').removeClass('active');
        $(this).addClass('active');
    });
}

main.js

import {foo} from 'custom'

foo();

ECMA SCRIPT 6 modules

Also notice that you need a module loader (like webpack/systemjs/requirejs etc.), as well as Babel that transpiles your ES6 code to ES5.

like image 142
Alon Avatar answered Apr 22 '26 19:04

Alon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!