Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-21-2012, 11:55 PM   PM User | #1
andyjason
New to the CF scene

 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
andyjason is an unknown quantity at this point
Trouble with links inside affected div

Hey all,

I'm doing an ajax website. When a link outside of the affected div is clicked on, then the ajax works, but if I click on a link inside the affected div, it doesn't. Please help. My js code's below:

Code:
             $(function () {
			if (window.location.hash){contentload(window.location.hash);}

			$('a').click(function() {
				fragment = this.hash;
				contentload(fragment);
			});
		});

		function contentload(fragment) {
			fragment = fragment.slice(1).replace('!', '')
			$('#divname').load('http://www.myurl.com');

		}
andyjason is offline   Reply With Quote
Old 03-22-2012, 09:54 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
With "affected div" you mean the div with id "divname" whose content changes by using the Ajax .load() method?

This is because .click() can only bind to elements that exists at the moment the method is called. Everything that is added afterwards (like by using the .load() method) will not be covered by this .click() handler.

Solution: With jQuery pre 1.7 you should use .live() and starting with jQuery 1.7 you should use .on()
Code:
$('a').live('click', function() {
	fragment = this.hash;
	contentload(fragment);
});

// OR

$(document).on('click', 'a', function() {
	fragment = this.hash;
	contentload(fragment);
});
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, hash, javascript, links

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:47 PM.


Advertisement
Log in to turn off these ads.