Search code examples
ajaxloadlivejquery

jquery: bind click event to ajax-loaded elmente? live() won't work?


hey guys, I have an input field that looks for matched characters on a page. This page simply lists anchor links. So when typing I constantly load() (using the jquery load() method) this page with all the links and I check for a matched set of characters. If a matched link is found it's displayed to the user. However all those links should have e.preventDefault() on them.

It simply won't work. #found is the container that shows the matched elements. All links that are clicked should have preventDefault() on them.

edit:

/*Animated scroll for anchorlinks*/
var anchor = '',
    pageOffset = '',
    viewOffset = 30,
    scrollPos = '';
$(function() {
    $("a[href*='#']").each(function() {
        $(this).addClass('anchorLink');
        $(this).bind('click', function(e) {
            e.preventDefault();
            //console.log('test');
            anchor = $(this).attr('href').split('#')[1];

            pageOffset = $("#"+anchor).offset();

            scrollPos = pageOffset.top - viewOffset;
            $('html, body').animate({scrollTop:scrollPos}, '500');
        })      
    });
});

Well, I'm looking for all href's that contain a #. So I know those elements are anchors that jump to other elements. I don't want my page to jump, but rather scroll smoothly to this element with this specific #id.

This works fine when I use bind('click', ... for normal page-elements that have been loaded when the page is opened. It doesn't work for anchors that have been loaded via ajax! If I change the bind to live nothing does change for the ajax loaded elements - they still don't work. However normal anchors that have always been on the page are not triggering the function as well. So nothing works with live()!


Solution

  • When you say "it won't work" do you mean that your function is not been called or that you can not cancel out of the function? As far as I know you can not cancel out live events. With jQuery 1.4 you can use return false to cancel out live event propagation. Calling e.preventDefault() won't work.

    Edit: right so this code should in principal work. What it still won't do is, it won't add the 'anchorLink' class to your new anchors. However if the clicks work then let me know and I will give you the right code to add the class too!

    var anchor = '',
        pageOffset = '',
        viewOffset = 30,
        scrollPos = '';
    $(function() {
        $("a[href*='#']").each(function() {
            $(this).addClass('anchorLink');
        });
    
    
        $("a").live('click', function(e) {
            if ($(this).attr("href").indexOf("#") > -1) {
                e.preventDefault();
                //console.log('test');
                anchor = $(this).attr('href').split('#')[1];
    
                pageOffset = $("#" + anchor).offset();
    
                scrollPos = pageOffset.top - viewOffset;
                $('html, body').animate({ scrollTop: scrollPos }, '500');
                //nikhil: e.preventDefault() might not work, try return false here
            }
        });
    });