SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    big problem in loop-for

    hi

    i have many pieces of code like:
    Code JavaScript:
     <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.

    Code JavaScript:
    [CODE]    <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>
    [/CODE]

    thanks

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yes there is a syntax error, you're mixing up single quotes and double quotes. This should work:
    Code 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);
                    }
                });
            });
        }

    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.

    Code javascript:
        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
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the bigger problem is exactly $m+x = [];

    Is not working, give an error.

    What is the correct syntax for that?

    thanks

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    There is no "correct" syntax per se, just name it however you want it.
    For example m_x would be okay
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am a bit confused

    Code JavaScript:
     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

    Code JavaScript:
            refreshChart0 = function() {
                chart1.updateSeries("January Visits", $m_0);
                chart1.render();
                stackedAreaLegend.refresh();
            }

  6. #6
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    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.

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there is an error:

    The Online Lint

    unterminated string literal

    eval('var fn = function(evt){

    thanks

  8. #8
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put all that code in one line.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you. You save my day



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
  •