Jump to content

JQuery - document ready - ajax - link clicks not working


Recommended Posts

I think this is a simple problem but I cannot work it out.

 

When an ajax page is loaded into the #ajax div the links are now longer triggered.

 

I think this is because the document is loaded and the function is run. When the new information comes into the page it isn't in the same scope as the click function. Is this correct? Is there a work around?

 

function pageload(hash) {
if(hash) {
	$.ajax({
		url: "index.php?p="+hash+"&logo=0",
		cache: false,
		success: function(html){
		     $("#ajax").html(html);
		}
	});
}
}

$(document).ready(function(){
$.historyInit(pageload, "index.php");

// set onlick event for buttons
$("a").click(function(){alert('click');
	if (strpos($(this).attr('href'), 'index.php') !== false) {
		var hash = idToPath($(this).attr('href'));						
		$.historyLoad(hash);
		return false;
	}
});
});

It's because the events on the elements are placed when the dom is ready. The events wont be placed on the elements you load with the ajax call.  There is a workaround though,

take a look at live()

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.