www.webdeveloper.com
Results 1 to 8 of 8

Thread: Concatenated Variable not retrieving variable information

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Location
    Newton Aycliffe, Co Durham, England
    Posts
    72

    Concatenated Variable not retrieving variable information

    The title of this thread might sound a bit odd, but it's the best way to describe my problem that I could think of.

    I'll show you the code first:

    function runAccordion(index)
    {
    var nID = "Accordion" + index + "Content";
    if(openAccordion == nID){
    nID = '';
    }
    ContentHeight = document.getElementById("Accordion" + index + "Content"+"_").offsetHeight;
    setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'"
    + openAccordion + "','" + nID + "')", 33);

    var copyhtml = "copy"+index;

    openAccordion = nID;
    alert(copyhtml);
    document.getElementById("copy").innerHTML=copyhtml;
    document.getElementById("img"+index).style.height=(accht-175)+"px";

    }

    Description: I have created an Accordion menu courtesy of "Switch on the Code". That runs great. I am trying to make the inner HTML of a div id="copy" correspond with the menu. Quite simple. Set several text strings as variables and append the variable index to the end of the text 'copy' to retrieve the relevant text. copy1, copy2, copy3 etc. That's the variable copyhtml's job. GetElementById then 'inserts' the text in the div.

    Here's the problem, which I really can't get my head around. If I type in 'copy2' for example on the line using getElementById for the div "copy" rather than the value of copyhtml, it works. It displays the correct text string (copy2). Even though the alert shows copyhtml to be copy2 (the same) it doesn't retrieve the string from the variable if I code the line using copyhtml. (Edit: It actually displays the words 'copy2') It's something to do with the concatenation I guess, but I just don't know what the problem is.

    Does that make sense?
    Last edited by Gledders; 01-27-2012 at 08:18 AM.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    it does kind of make sense and if I understand what the problem is, it is that you can't refer to a variable just using a string, you have to let the code know you're looking for a variable. I'm not sure that I follow the rest of your code, but try this and see if it makes any difference:

    Code:
    document.getElementById("copy").innerHTML=window["copy"+index];

  3. #3
    Join Date
    Dec 2010
    Location
    Newton Aycliffe, Co Durham, England
    Posts
    72
    That did it! I've Googled loads of info on variables but never seen that. Can anybody explain why?

    Xelawho, I am very, very grateful. Thankyou.

  4. #4
    Join Date
    Nov 2010
    Posts
    1,084
    Quote Originally Posted by Gledders View Post
    I've Googled loads of info on variables but never seen that. Can anybody explain why?
    probably a case of needing to know what you're looking for to be able to find it

    a search for "dynamic variables" or "variable variables" turns up some info... and some very lame arguments trying to say that eval() is better suited to this purpose

  5. #5
    Join Date
    Dec 2010
    Location
    Newton Aycliffe, Co Durham, England
    Posts
    72
    Xelawho. Developing this idea further because it could be generally useful, is to retrieve values from an array. I hit the same problem again.

    Instead of setting the text as a variable, I would like to set it as part of an array. Then I can alter font sizes, add matching images to the page etc. Therefore I created several two part arrays. copy1[0], copy1[1], copy2[0], copy2[1] etc.

    Then I wish to pull the text out of the array, which typed in as copy1[0] works perfectly. However window["copy"+index+"[0]"] doesn't. It alerts back as undefined.

    Tried window["copy"+index]+"[0]"], window["copy"+index][0] and everything else I could think of. Eventually this passes variables from the array to a function I have written that sets inner HTML. the correct font size (evaluated from the browser dimensions) and matching images (eventually) which is reasonably sophisticated stuff, yet I keep getting stuck on the basics. It's really frustrating.

    Can you shed any light for me please?

  6. #6
    Join Date
    Nov 2010
    Posts
    1,084
    Quote Originally Posted by Gledders View Post
    Tried window["copy"+index][0]
    are you sure? because this alerts "big" as it should...
    Code:
    <script type="text/javascript">
    var index=1
    var copy1=["big","small"]
    alert(window["copy"+index][0])
    </script>

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