Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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');
    
    		}

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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);
    });


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •