0

I am trying to pass a parameter in a function, which I will use in a Highcharts file.

function getID (ID) {
    chart+ID+.series[0].update({
        ....do something
    )};
}

When I try to concatenate the ID that I get from the function, to the update call; I get an error saying

unexpected token "."

How do I use the ID in the update function? I thought that the + sign would concatenate strings, so

chart+ID+.series[0]

is the same as, with ID='1', to

chart1.series[0] 
  • it does that is not a string though, is chart part of an object or array, such as someObj.chart – ArtisticPhoenix Jul 07 '14 at 21:33
  • so how do you actually include a parameter, in a case like this? –  Jul 07 '14 at 21:34
  • you can do someObj['chart'+ID] – ArtisticPhoenix Jul 07 '14 at 21:35
  • To answer the question, you'll have to show what "chart" is. It's probably not possible to do without some seriously undesirable hackery, so if you add more context to your question a better solution can be suggested. – Pointy Jul 07 '14 at 21:36
  • Not that much of an expert; do I use the Obj keyword and close the whole API call in square brackets? –  Jul 07 '14 at 21:36
  • @newbiez no, that suggestion would require a different setup (and possibly the proper approach). – Pointy Jul 07 '14 at 21:36
  • Pointy: "chart" is the name of the Highcharts chart that I have created; I have multiple charts on the same html file, so I need to update these charts and instead than write 10 times the same code, which is the same except the name of the chart, I am passing a parameter with a number used as ID. –  Jul 07 '14 at 21:37
  • try this window['chart'+ID].series[0], possible duplicate of http://stackoverflow.com/questions/918995/how-to-concatenate-var-names-in-javascript – ArtisticPhoenix Jul 07 '14 at 21:39
  • ArtisticPhoenix: Sadly it won't work; I get an error (it consider the content of the square bracket as undefined). EDIT: to make it work I need to put only 'chart' in quotes; probably you made a typo with the quote on ID –  Jul 07 '14 at 21:42
  • ^yea there was a typo for about 30 seconds, you must have tried it then – ArtisticPhoenix Jul 07 '14 at 21:50

1 Answers1

2

If chart1 is a global variable, you can do:

window['chart'+ID].series[0].update(...);

because global variables are automatically turned into properties of the window object.

If it's not a global variable, you can't do this. It would be best to make chart an array, rather than having chart1, chart2, etc. Then you could do:

chart[ID].series[0].update(...);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Bingo....I have it declared as global variable and now it works fine! So what is the "window" at the beginning doing? –  Jul 07 '14 at 21:47
  • I explained that. All global variables are implicitly properties of the `window` object, which represents the context of a web page in Javascript. – Barmar Jul 07 '14 at 21:50
  • 1
    But you really should change to using an array, even if it is a global variable. Accessing variables this way is poor coding style. – Barmar Jul 07 '14 at 21:51
  • Sorry, I was not aware of what this window object is; I went to read about it (I am new to JS). Indeed it makes sense to use an array; I need to access in various places to this variable and parameter, so it makes more sense to use an array. Thanks! –  Jul 07 '14 at 22:49