tags:

views:

53

answers:

3

Can someone please look at this javascript function and tell me tell me what I am doing wrong? I want to pass in 4 variables. I know very little javascript.

Thanks.

  function chg(back,front,left,right)
  {
      return test("div#post", "#" + "back", "#" + "front", left, right);
  }


Here is the onload file

window.onload = function() {
  function chg(back,front,left,right)
  {
      return test("div#post", "#" + "back", "#" + "front", "left", "right");
  }
}
+2  A: 

Nothing is syntactically wrong that I can see.

  • Did you mean back instead of "back"? ( same with front )
  • Is there a test function defined in an outer scope? What does it return?
  • Are the 4 arguments being populated?
  • Can you provide more code?
meder
Yes there is a test function in another file. Here is how I have this working now. This test function is in a file that handles all of the onLoad events. The reason that I want to put this into a function is so that I can dynamically change the colors and padding. Now I'm wondering if because this is an onload even that I can't do this. Is this possible? BTW: thank for the help.
Jim
you will not get a definitive answer without posting actual real code.
meder
I posted it above
Jim
When are you calling `chg`? Where is `test` defined? Did you mean `back` instead of `"back"`? Did you read anything I wrote in my answer?
meder
A: 

You have done nothing wrong syntactically. However, two of your input arguments (back and front) are never used - you are putting the words "back" and "front" in a string literal, so those arguments are never used. Functionally, it's impossible to say. What is test()? Is it another function declared elsewhere? If so, it would be helpful to see that one too.

Rex M
Hi Rex, thanks for the help. Let me ask this. Can this function be used dynamically in an onload situation? I want to change these values on the fly.
Jim
It's hard to tell with this amount of context, but there should be no reason why not.
Ben
+1  A: 

For all intents and purposes, my code and your code do the exact same thing. Maybe this will help?

  function chg(back,front,left,right)
  {
      var result;

      //Pass left & right to the test function
      result = test("div#post", "#back", "#front", left, right);

      //Do something with back & front....
      //What?... I have no idea.

      return result;
  }
J.Hendrix
Thanks Jimmy. I'm not a javascript guy by any means and maybe the problem is the "back" and "front" variables but as far as I thought, the test function is accepting them. I don't see a need for a return either. Am I wrong?
Jim
Like I said, my code and your code do the exact same thing. If you don't see a need for a return, you should take a second look at your code. Try telling us in words what you are trying to do. For example "I want to dynamically set the height, width and position of a div onload"
J.Hendrix