0

In my script.js I have this:

$(function () {
   function addColors(basicColors) {
      ...
   }
});

And in HTML:

<head>
<script src="scripts/script.js"></script>
</head>
<body>
   ...
<script>
   ...
   addColors(basicColors);
</script>
...

But in console i am getting

Uncaught ReferenceError: addColors is not defined

The script file is correctly referenced as other functionality defined in that file is working just fine.

1 Answer 1

4

It's a scope issue. addColors is defined as a local function within the anonymous function being passed to jQuery to execute when the page loads (in $(function() { ... })). Outside that function, addColors isn't defined.

If you want addColors to be global, you can define it before the $(function() { ... }) in script.js.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.