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

224
Views
Drupal 8 - I cannot call a function in another JavaScript file

I have two JavaScript files on my custom theme. One file has functions that need to be used on the other file. What I have below doesn't work. This is the error I get.

Uncaught ReferenceError: test is not defined.

jQuery will be used on both files.

file1.js

/**
 * @file
 * UI behaviors
 */

(function ($, Drupal) {

 'use strict';

  $(document).ready(function () {
    function test(){
     console.log('hello'):
    }
  });

})(jQuery, Drupal);

file2.js

/**
 * @file
 * UI behaviors
 */

(function ($, Drupal) {

  'use strict';

  $(document).ready(function () {
     test();
  });

})(jQuery, Drupal);

customtheme.libraries.yml

scripts:
  js:
    js/file1.js: {}
    js/file2.js: {}

customtheme.info.yml

libraries:
 - 'customtheme/scripts'
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your test function is not in the global scope.
If you want your test function to be able to be called from anywhere, don't put it in the $(document).ready or in the IIFE.
ie. in the first file, just have..

function test(){
  console.log('hello);
}

You may also want to namespace your functions, but that is a separate question I suppose.

Also, for Drupal, you may be wanting to use drupal.behaviors rather than $(document).ready (for the second file)

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!