0

I need some help, I can't identify or the code doesn't recognize me the element i'm looking for, to add a value. I have an iterator that create multiple selects, and I have another select that gets filled after the first ones are created. So the problem is when I want to fill the second ones, they don't get filled, I don't know why. Here is the iterator code to create multiples select inside my code:

$.getJSON("/ajax/load_files.php",{req:id_req},function(data){
rows=data['id_art'].length;
for(i=0;i<rows;i++){
    <select class="form-control agent'+i+'" id="sel_agent_'+i+'" disabled style="margin-top: 0px">
        <option value="0">No available</option>
    </select>

And here's my getJSON to receive the information to get filled after creating the select: (Obiously the getJSON works and returns me everything ok, but I can't set the second select with the received data from the getJSON:

$.getJSON("/ajax/search_agents.php",{id_prov:data["idprov"][i]},function(data5){
    if(data5!==false){
        total=data[0].length;
        var div5="";
        for(var c=0;c<total;c++){
            div5 += '<option value="' + data5[0][c] + '">' + data5[1][c] + '</option>';
        }
        $("#sel_agent_"+i+"").html(div5);
    }else{
        $("#sel_agent_").val("Prov. not loaded");
    }
});

I get this JSON as a result [["28"],["David Beckham"]] (it's not problem) I think that the problem it's in this line: $("#sel_agent_"+i+"").html(div5); Since it's not filling it correctly, I've just checked the select id and everything and it's correct.

Becerra95
  • 33
  • 1
  • 1
  • 6
  • It's somewhat confusing, there's no `data` defined inside the callback, so it's coming from outside, yet you first have `data["idprov"]` then you have `data[0]`, is it an object, which strangely has a property named `0`, or is it an array ? – adeneo Jun 09 '17 at 21:22
  • Yeah, all of that it's inside of another loop..... `code`$.getJSON("/ajax/load_file.php",{req:id_req},function(data){`code` – Becerra95 Jun 09 '17 at 21:39
  • Why do people write `+""` after concatenations? I've never gotten a good explanation for it. – Barmar Jun 09 '17 at 22:03
  • Where does the variable `i` come from in the `$.getJSON` callback function? Is it from the `for` loop at the top? – Barmar Jun 09 '17 at 22:04
  • @Barmar - It's from the loop inside the main JSON, the one that creates the selects – Becerra95 Jun 09 '17 at 23:01
  • Then the duplicate question should help you. The problem is that the closures in the callbacks don't have the current value of `i`. – Barmar Jun 09 '17 at 23:03
  • The getJSON to fill the selects it's inside the loop of the i value too, first it creates the selects, after that its supposed that fills them, but it doesn't. Both are inside the same loop – Becerra95 Jun 09 '17 at 23:05
  • Hmmm, i see you closed my thread, well so sad, can't get resolved or helped – Becerra95 Jun 09 '17 at 23:08

0 Answers0