jquery for loop to create multiple buttons

I need to create a set amount of buttons using jquery. I've tried a for loop and a while loop but this isn't working.

I'm storing the amount of pages I need in a variable 'pages', which when using console.log(pages) correctly shows how many buttons I require yet I still can't get the loop to work.

while (i <= pages) {
    pageButtons.append('<input type="button" id="button'+i+'" value="Random'+i+'"/>');
    i = i + 1;
}

I currently have the above code..

Answers


What is pageButtons assigned to? If you contain all of the buttons in a div, this will work

var pages = 5;
var pageButtons = $('#pageButtons');
for (var i = 0; i < pages; i++) {
  pageButtons.append('<input type="button" id="button' + i + '" value="Random' + i + '"/>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="pageButtons">

</div>

You would want to do something like this:

var pages = 5;
for (var i = 0; i <= pages; i++) {
  $('#buttons').append('<input type="button" id="button' + i + '"value="Random' + i + '"/>');
}

http:////jsfiddle.net/clccmh/x545y8re/


Need Your Help

How to rename a file using Python

python file-rename

I want to change a.txt to b.kml.

copying pdf file to clipboard with VBA

excel pdf vba clipboard

I have an excel macro which creates a PDF to file of an excel document.