Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: how to to pass array[x] to a variable?

  1. #1
    Join Date
    Dec 2010
    Location
    California
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default how to to pass array[x] to a variable?

    I want to pass array[x] to a variable so that I can use the variable in .innerHTML. I've researched and not found a reference.

    Any help appreciated. Big thanks.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Do you mean how can you pass it in a function?
    Code:
    (function(value){
        document.getElementByid('element').innerHTML = value;
    }(array[x]));
    Jeremy | jfein.net

  3. #3
    Join Date
    Dec 2010
    Location
    California
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    ok, let me show you my function that doesn't work:

    Code:
    function captionToDiv() {
    document.getElementById("captionHere").innerHTML = "captionsList[caption]";
    return false;
    }
    can we start here?

  4. #4
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Code:
    function captionToDiv() {
    document.getElementById("captionHere").innerHTML = captionsList[caption];
    return false;
    }
    Should work, aslong as you make sure captionsList is defined before you call captionToDiv(). If you want to pass it as a parameter:
    Code:
    function captionToDiv(val) {
    document.getElementById("captionHere").innerHTML = val;
    return false;
    }
    captionToDiv(captionsList[caption]);
    Jeremy | jfein.net

  5. #5
    Join Date
    Dec 2010
    Location
    California
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    The function to create and fill the array runs before I call captionToDiv().

    Did you mean for me to use the code you have highlighted?

  6. #6
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Ignore the highlighted, I was just pointing out the variable.
    Jeremy | jfein.net

  7. #7
    Join Date
    Dec 2010
    Location
    California
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    here is the computed code which returns error: missing ) after formal parameters. Pointing to line 1.

    Code:
    function captionToDiv(captionsList[caption]) {
    document.getElementById("captionHere").innerHTML = "captionsList[caption]";
    return false;
    }
    I don't see where ")" is needed.

  8. #8
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    No - change your code to:
    Code:
    function captionToDiv() {
    document.getElementById("captionHere").innerHTML = captionsList[caption];
    return false;
    }
    Jeremy | jfein.net

  9. #9
    Join Date
    Dec 2010
    Location
    California
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    receiving error: captionsList not defined. It is defined in the <head> and also in the function that creates the array, and also here:
    Code:
    function captionToDiv() {
    var captionsList;
    document.getElementById("captionHere").innerHTML = captionsList[caption];
    return false;
    }
    am I missing something?

  10. #10
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Take the var captionList; away. Paste all of your code on your page here please.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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