Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
using functions across multiple scripts/functions

I have a handful of js scripts in jquery:

vars.js
 $(function(){
      function yesOrNo(val){
          if (val == "yes"){
              return "yes";
          }
          if (val == "no"){
              return "no";
          }
 });

scripts1.js


 /// include(vars.js) <-- what would go here?

   $(function(){
       
       var var1 = "yes";
       var test1 = yesOrNo(var1);
       alert(test1);
   });

scripts2.js

  
 /// include(vars.js) <-- what would go here?

   $(function(){

       var var2 = "no";
       var test2 = yesOrNo(var2);
       alert(test2);
   });

scripts3.js

 /// include(vars.js) <-- what would go here?

   $(function(){
       var var3 = "yes";
       var test3 = yesOrNo(var3);
       alert(test3);
   });

How can i call the function yesOrNo from a different scripts.js page?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want to add another file add type="module" parameters in your main code, then add the line import {yesOrNo} from './vars.js'; in your code. Then, in the vars.js file, add the function as export function yerOrNo, not function yesOrNo.

My edit of your code:

vars.js

export function yesOrNo(val){
  if (val == "yes"){
    return "yes";
  }
  if (val == "no"){
    return "no";
  }
}

main.js

import {yesOrNo} from './vars.js'
$(function(){
  var var2 = "no";
  var test2 = yesOrNo(var2);
  alert(test2);
});

On your html call main.js like that

<script src="main.js" type="module"></script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!