0

I try to use for loop inside an AJAX callback, but I can't get it to work.

function swapsite(e){
  $.ajax({
              type: "POST",
              url: "../queryadmin.inc.php",
              data: {site:e}
            }).done(function( msg ) {
             console.log(msg);
             var result=JSON.parse(msg);
             console.log("----------------");
             console.log(result);
              $('#draggable').html("<p id='article"+result[0]['id']+"'>"+result[0]['title']+"</p>");
              var i=0;
              for(i=2,i<=7,i++) $('#draggable'+i).html("<p id='article"+result[i]['id']+"'>"+result[i]['title']+"</p>");

            });

  }

The problem is with the line starting with for

Rápli András
  • 3,869
  • 1
  • 35
  • 55
  • What is not working? Check your console for errors. And post some more code. The code you've shown us looks ok except for that you have to use semicolons `;` in `for` loop (typo when posting the question?). – freakish Aug 13 '13 at 14:04
  • 3
    Use semicolons **;**, not commas **,** in the **for**. – Kneel-Before-ZOD Aug 13 '13 at 14:07

1 Answers1

7

That's incorrect syntax for a Javascript for loop. You need to use semi-colons and not commas:

for(var i=2; i<=7; i++)

Correct syntax from MDN:

for ([initialization]; [condition]; [final-expression])
   statement
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176