Hey guys.
Having a little trouble finding the answer to this one...
I have a series of form elements with id's, which, when clicked, I want to trigger another event...
I have a set of variables, each container an array of data.
The variable names are the same as the id's of the form elements.
I want to pass the id of the clicked element, as the variable name of the data array that I need to send....
in PHP I would do this like ${'string'} to access the variable $string
I've found pages telling to do something like window['string']...but that doesn't seem to do....well, anything in my case.
I am getting the correct string from the element id.... (let's say... 'foo'...) how do I tell my script I want it to use the contents of the variable foo?
window[variableName] works for variables declared in the global namespace ( the window object ): in other words, for variables declared outside of a function or inside a function but without the var keyword.
Honestly I don't see exactly what you're trying to do but this should help.
Ok, well, removing the 'var' accomplished what I need for the moment.
I have no idea what, if any, the implications of doing so are...
For reference, I am using a javascript charting library (highcharts), and want to allow the user to select series to add/remove from the chart using a row of check boxes.
The variable name containing the data for each series, the id of the series once added to the chart, and the id of the check box are all the same.
So I wanted to be able to use the id of the check box to tell the script which dataset to add or remove...
Personally I'd put the data sets either as a TABLE element in the markup, for accessibility reasons, or in private variables to prevent messing up with them.
racerX:
code:
var objs=[]
set1 = {name:'Set 1',data:[1,2,5,8,9,6,3,4,7,8],color:'#c66',id:'set1'};
/* ... */
objs[set1.id]=set1
That's one convoluted way of doing:
code:
var objs={};
objs.set1 = {name:'Set 1',data:[1,2,5,8,9,6,3,4,7,8],color:'#c66'};
or a more direct object declaration like the one in the first jsfiddle link.
which works beautifully for me here. is there a downfall to this syntax compared to what you guys have presented?
As far as the table idea - I really don't want to get into that in this context.
The page is a metrics dashboard on a local intranet. The space is very precisely allocated, and the sole purpose of it is to display the data graphically.
There are data tables where appropriate, and the data is available in a variety of formats in a variety of places.
If anyone has an issue viewing it, then we need to correct their setup...
is there an javascript equivalent of php's print_r or var_dump?
It's very frustrating trying to check whether I am getting the correct array contents with some of this stuff...
I'm feeling out how to go about selecting the right things, and I want to be able to just spit the array out on the page for verification.
Everything I've found online on the subject is more complex than I want to deal with
if you've got a debugging tool in your browser, console.log( anyVariable ); should output just what you want.
I'm not familiar with the debugging tool of other browsers, but in Opera DragonFly ( press Ctrl+Shift+I to open it ), in the Scripts, there is a command line and an Inspection window where you can type functions and variable names and inspect them and their properties in the Inspection window.