www.webdeveloper.com
Results 1 to 3 of 3

Thread: array values are being passed as variable names?

  1. #1
    Join Date
    Sep 2008
    Posts
    95

    array values are being passed as variable names?

    Hello,

    I'm trying to add a function call to an array to be used later. It works when passing numbers/strings as parameters [e.g. "myfunction(1, 'x')" is added and called successfully later]. However, I need to store an array of values as a parameter in the function for use later [e.g. myArray=[string0,string1,string2]; myfunction(1, 'x', myArray)]. But it looks for an object string0 when called as opposed to just passing the string as a string.

    Here's a simplified version of what i'm trying to do in code:

    Code:
    var holdFunction = new Array();
    function f1(){
    
    var var1=1; var var2='x'; myObj.innerHTML='test'; var myArray=[myObj.innerHTML, var1, var2]; forLater("f2(1,'xx', "+myArray+")");
    } function forLater(fct){
    holdFunction.push(fct);runFct();
    } function runFct(){
    //alert(holdFunction[0]); eval(holdFunction[0]);
    } function f2(v1,v2,v3){
    ...
    }
    If f1 is called...

    The alert box (if uncommented) shows:
    f2(1,'xx',test, 1, x)
    And the line to run the script prompts the error:
    Error: 'test' is undefined
    But I need the array to stay an array with static text/number values to manipulate as an array in f2. I feel there's a simple solution I'm overlooking. Any ideas would be appreciated.

    Regards,
    Josh
    Last edited by speghettiCode; 05-19-2009 at 06:26 AM.

  2. #2
    Join Date
    May 2009
    Posts
    150
    The simplest way would be:

    Code:
    var var1=1;
    var var2='x';
    myObj.innerHTML='test';
    var myArray=['"' + myObj.innerHTML + '"', var1, '"' + var2 + '"'];
    forLater("f2(1,'xx', "+myArray+")");
    Which will insert quotes into the strings to be parsed as string enclosing quotes later.

  3. #3
    Join Date
    Sep 2008
    Posts
    95

    [RESOLVED]array values are being passed as variable names?

    Hello Y_Less,

    That's what I needed. I'm a bit embarassed by the simplicity of the solution i had overlooked; but I'm glad it's working. Thanks!

    Regards,
    Josh

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles