0

I am having trouble passing a parameter to a function.

Here is my code (edited to show flow and positioning in the html doc):

<!-- this src file precedes -->
    // this resides in a js src file on its own and as is
    function mySpecialFunction(thisIndex) {
      alert(thisIndex); // shows as blank
    }

<!-- this src file follows -->
$(document).ready(function() {
  $(window).load(function() {
    $(document).on('change', '.jq__pAC', function(event) {
    var i = 1;
    // some JavaScript code
    mySpecialFunction(i);
    // some more JavaScript code
    }); // end jq__pAC
  }); // end .load()
}); // end .ready()

my alert() event displays blank.

The 2 blocks of js code are in separate src files. Does that have anything to do with it?

7
  • Works fine here jsfiddle.net/j08691/k4s46 Commented Apr 28, 2014 at 20:47
  • Yeah, there's absolutely nothing wrong with that code. What browser are you using? -- I see they are in different files. Simply include the function before you include the call for it. Commented Apr 28, 2014 at 20:48
  • sure does @j08691 -- and even to my "eyeball" I see nothing wrong with it. Commented Apr 28, 2014 at 20:48
  • 2
    It doesn't matter if the code is in 2 different places, you just need to make sure that the file which defines the function is called first. Commented Apr 28, 2014 at 20:49
  • Depends on the order of your JavaScript files. Commented Apr 28, 2014 at 20:51

1 Answer 1

4

If you say the functions are in different files, probably the order of the files in the problem.

Make sure that the calling method (mySpecialFunction(i);) comes after the declaration (function mySpecialFunction(thisIndex)).

EDIT:

In your edit you have $(document).ready and $(window).load. Remove the last one and it will work.

See jsfiddle.

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

12 Comments

is javascript not like PHP where the functions can live wherever @PatrickHofman?
No. It is not. You have to declare it before you use it.
ok, thanks -- that's probably my problem. I'll check @PatrickHofman
Not if they're in separate files, though. The function itself must be declared before the call, if they reside in separate files.
@FelixKling: I didn't think so when using multiple files. In the same file it does.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.