I'm trying to make a page that loads results from an external PHP script and then dynamically adds buttons which when clicked takes you to the other pages. I
figured I'd do this via a for
loop that gets the number of pages from the PHP file (which I've omitted).
I have an additional function called test(pageNum)
that takes in a page number parameter and gets the results and displays them in the table. That functions fine but whenever I load the page it seems to call the button click functions immediately. Could someone explain the issue here please?
$(document).ready(function() {
test(1);
$.get("link here", {
op: "pages"
}, function(data) {
$("h1").html(data);
for (var j = 1; j < data.length; j++) {
(function() {
var btn = $("<button>/", {
type: 'button',
text: j,
on: {
click: test(j)
}
});
$('#buttons').append(btn);
})(j);
}
});
});