hi
i have many pieces of code like:
<script type="text/javascript">
dojo.query("body").delegate("#input0 > select.estatistica", "onchange", function(evt){
dojo.xhrPost({
url: "drop2.php",
handleAs: "json",
postData: "data=" + $(this).val(),
preventCache: true,
load: function(json) {
$m0 = [];
for (var i = 1; i < 10; i++) {
$m0.push(parseFloat(json[i]["valor" + i]));
}
dojo.addOnLoad(refreshChar0);
}
});
});
</script>
<script type="text/javascript">
dojo.query("body").delegate("#input1 > select.estatistica", "onchange", function(evt){
dojo.xhrPost({
url: "drop2.php",
handleAs: "json",
postData: "data=" + $(this).val(),
preventCache: true,
load: function(json) {
$m1 = [];
for (var i = 1; i < 10; i++) {
$m1.push(parseFloat(json[i]["valor" + i]));
}
dojo.addOnLoad(refreshChart1);
}
});
});
</script>
i tried this loop, but i am not sure about the script. Probably i have syntax errors.
<script type="text/javascript">
for(x=0; x<10; x++) {
dojo.query("body").delegate("'#input'+x+'> select.estatistica'", "onchange", function(evt) {
dojo.xhrPost({
url: "drop2.php",
handleAs: "json",
postData: "data=" + $(this).val(),
preventCache: true,
load: function(json) {
$m+x = [];
for (var i = 1; i < 10; i++) {
$m+x.push(parseFloat(json[i]["valor" + i]));
}
dojo.addOnLoad(refreshChart+x);
}
});
});
}
</script>
thanks
Yes there is a syntax error, you’re mixing up single quotes and double quotes. This should work:
for(x=0; x<10; x++) {
dojo.query("body").delegate("#input"+x+" > select.estatistica", "onchange", function(evt) {
dojo.xhrPost({
url: "drop2.php",
handleAs: "json",
postData: "data=" + $(this).val(),
preventCache: true,
load: function(json) {
$m+x = [];
for (var i = 1; i < 10; i++) {
$m+x.push(parseFloat(json[i]["valor" + i]));
}
dojo.addOnLoad(refreshChart+x);
}
});
});
}
However, I wouldn’t use a loop at all, but [id^='input']
, which means “Any element whose id starts with ‘input’”. Simply because it’s easier to read and automatically scales if there are ever more (or less) than 10 elements.
dojo.query("body").delegate("[id^='input'] > select.estatistica", "onchange", function(evt) {
dojo.xhrPost({
url: "drop2.php",
handleAs: "json",
postData: "data=" + $(this).val(),
preventCache: true,
load: function(json) {
$m+x = [];
for (var i = 1; i < 10; i++) {
$m+x.push(parseFloat(json[i]["valor" + i]));
}
dojo.addOnLoad(refreshChart+x);
}
});
});
The for loop should be faster btw (in theory, haven’t tested it)
Also, $m+x is not a valid variable name (+ are not allowed in variable names). Might want to change that
the bigger problem is exactly $m+x = ;
Is not working, give an error.
What is the correct syntax for that?
thanks
There is no “correct” syntax per se, just name it however you want it.
For example m_x would be okay
i am a bit confused
load: function(json) {
$m_x = [];
for (var i = 1; i < 10; i++) {
$m_x.push(parseFloat(json[i]["valor" + i]));
}
dojo.addOnLoad(refreshChart+x);
}
This $m_x = ; is equivalent to $m_1 = ; $m_2 = ; $m_3 = ; and so on, correct?
and this: dojo.addOnLoad(refreshChart+x); is equivalent to dojo.addOnLoad(refreshChart+1); dojo.addOnLoad(refreshChart+2); …
It is correct?
at the moment i get a ReferenceError: refreshChart is not defined
refreshChart0 = function() {
chart1.updateSeries("January Visits", $m_0);
chart1.render();
stackedAreaLegend.refresh();
}
Variable “x” doesn’t exist in that code because its being executed in event handler, not inside that loop, so you’ll need a different approach.
I’d go with a big eval() like this:
for(var x=0; x<10; x++)
{
eval('var fn = function(evt){
dojo.xhrPost({
url: "drop2.php",
handleAs: "json",
postData: "data=" + $(this).val(),
preventCache: true,
load: function(json) {
$m' + x + ' = [];
for (var i = 1; i < 10; i++) {
$m' + x + '.push(parseFloat(json[i]["valor" + i]));
}
dojo.addOnLoad(refreshChar' + x + ');
}
});
};');
dojo.query("body").delegate("#input" + x + " > select.estatistica", "onchange", fn);
}
It takes that function’s code, replaces number with value of x and assigns it to variable fn, then fn is being assigned as event handler to correct item. There might be typos in that code or it might not work at all.
there is an error:
The Online Lint
unterminated string literal
eval('var fn = function(evt){
thanks
Put all that code in one line.
thank you. You save my day