0

I have written a jQuery script. This should, as I've thought it, take the number of templateTotal (the total number of Templates which exist on USB) from the JSON and generate as many buttons, as templateTotal indicates.

For example if templateTotal is 6, then the script should generate 6 buttons.

(function poll() {
    setTimeout(function () {
        $.ajax({
            type: "GET",
            url: "/api/status",
            processData: true,
            dataType: 'text',
            cache: false,
            success: function (data, textStatus, request) {
                var template = jQuery.parseJSON(data);
                var templateButton = "";
                for (i = 1; i > templateTotal; i++);
                templateButton +=  '<button class="templateButton" id="T' + i + '" formaction="/api/template" post="template=' + i + '">T' + i + '</button>'
            },
            complete: poll
        });
    });
})();
Spokey
  • 10,974
  • 2
  • 28
  • 44
user234110
  • 107
  • 6

2 Answers2

3

You have a semi colon after the for loop:

 for(i=1; i > templateTotal; i++);

It should be:

 for(i=1; i > templateTotal; i++)
                templateButton += '<button class="templateButton" id="T' 
               + i     +'"
               formaction="/api/template"post="template='+i+'">T'+i+'</button>'

or

for(i=1; i > templateTotal; i++){
    templateButton += 
    '<button class="templateButton" id="T' +   i +
    '" formaction="/api/template"   
     post="template='+i+'">T'+i+'</button>'
}

The semicolon indicates the end of a statement (as does the carriage return). The for loop (unless followed by brackets) will execute the next single statement which the semicolon ends.

A semicolon isn't necessary to end a statement, but it's good practice to put it in. Omitting it will still create valid syntax.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • @user234110 you are just creating a string, you forgot to append it `.append(templateButton)` or `.html(templateButton)` – Spokey Jul 23 '14 at 13:26
  • and it will comes where? sry... am a newbew ) – user234110 Jul 23 '14 at 13:34
  • This will get you started in the right direction: http://stackoverflow.com/questions/3015335/jquery-html-vs-append Now that you have created the HTML you have to append it to the DOM in the page. – kemiller2002 Jul 23 '14 at 13:36
0

ur code has 2 errors ,

1)Semicolon after for

means for loop will be execured templateTotal times And do nothing.Then

  templateButton += '<button class="templateButton" id="T' + i +'" formaction="/api/template" post="template='+i+'">T'+i+'</button>'

This code runs once only.So remove semicolon.

2)Also the code

  templateButton += '<button class="templateButton" id="T' + i +'" formaction="/api/template" post="template='+i+'">T'+i+'</button>'

is missing ; semicolon.

Spokey
  • 10,974
  • 2
  • 28
  • 44
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73