0

I'm having a problem with this function:

var url = "";

function multiSearchTest() {
    var formData = $("#frmSearch").serialize();
    var look = new Array("SDO", "AR", "AS", "AC", "AP", "GEMO", "CC");
    var count;

    for(count = 0; count < look.length; count++){
       url = "index.php?Page&module=mod_page&action=dispatch&todo=cerca"+look[count]+"&" + formData;
       console.log(url);
    var test = "#"+look[count]+"result";

    $.get(url, function(data) {
           $(test).html(data);
           console.log("TEST VAL => " + test);
        });
    }
}

As you can see the url construction is perfect...but when I say where to print the output it return me only the last val of the array. Any ideas or suggestion? Where is my mistake?

1
  • you have declare test as a variable, but you are assigning multiple value to it. so make test as an array . then only it can store all those values . Commented Jun 19, 2014 at 9:04

1 Answer 1

2

Declare var test outside of forloop. and make it as array.beacuse the var test in for loop every time it creates a new object so declare the variable test in outside of for loop

 function multiSearchTest() {
        var formData = $("#frmSearch").serialize();
        var look = new Array("SDO", "AR", "AS", "AC", "AP", "GEMO", "CC");
        var count;
    var test = [];

        for(count = 0; count < look.length; count++){
           url = "index.php?Page&module=mod_page&action=dispatch&todo=cerca"+look[count]+"&" + formData;
           console.log(url);
           test[count] = "#"+look[count]+"result";

        $.get(url, function(data) {
               $(test[count]).html(data);
               console.log("TEST VAL => " + test[count]);
            });
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to sudharsan and Amit :) Problem Solved

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.