Reputation: 455
I load a page in a div through AJAX. This works great. On this ajax loaded page there are some buttons (generally created with javascript). When I click on one of these buttons the page refreshes. Weird thing is; no where I stated the page SHOULD refresh. Afterall; that's why I use ajax.
This is the javascript that's executed on the AJAX loaded page:
function buttonClicked(id){
var page = id;
photoPage = page*10;
if(cur_button < id)
{
minCount += 10;
maxCount += 10;
}
else
{
minCount -= 10;
maxCount -= 10;
}
cur_button = id;
jQuery("#imagesDiv").html("");
$( "#imagesDiv" ).append( " <br/>");
executePage();
}
function createButtons() {
var i = 1;
var button = "";
while(i <= photoCount)
{
var button = document.createElement("BUTTON");
var buttonName = document.createTextNode(i);
button.appendChild(buttonName);
button.id = i;
jQuery(button).bind('click', { id: i}, function(event) {
var data = event.data;
buttonClicked(data.id);
});
imagesDivJS.appendChild(button);
i++;
}
}
function executePage()
{
setImagesDivID();
createButtons();
$( ids ).append( " <br/>");
populateDiv();
}
function populateDiv() {
for(var i = minCount;i < maxCount; i++)
{
if(i < total_count)
{
create_image("../"+photos[i],photoAlts[i]);
$("#"+ids).append( "<p style=\"display:inline; padding-left:10px;\">" + photoTags[i] + "</p><br/>" );
}
}
}
Help will be appreciated. I've been breaking my neck around this thing!
Upvotes: 0
Views: 111
Reputation: 4305
The button is refreshing the page because it is a submit button. You need to add an attribute of type="button"
to the button. For example:
function createButtons() {
var i = 1;
var button = "";
while(i <= photoCount)
{
var button = document.createElement("BUTTON");
var buttonName = document.createTextNode(i);
button.appendChild(buttonName);
button.id = i;
// Add type="button"
button.setAttribute("type", "button");
jQuery(button).bind('click', { id: i}, function(event) {
var data = event.data;
buttonClicked(data.id);
});
imagesDivJS.appendChild(button);
i++;
}
}
Upvotes: 1