ApPeL
ApPeL

Reputation: 4921

jQuery on or live?

I recently deployed an infinite scroll to an app that I have build and found that sometimes I need to click twice for something to happen.

My app has likes, and once the dom had loaded, i need to click on the like button twice before it changes, then once i click on the other ones it's okay but I always have to click once for the app to almost "wake up"

Is there a better solution?

$(document).ready(function() {
    function runUpdate(url, item) {
        $.ajax({
            type: "GET",
            url: url,
            cache: false,
            success: function(data){
                if (data == '200') {
                    removeAddColor(item);
                }
            }
        });
    }

    $('.mini-like').live('click', function(){
        $('.mini-like').toggle(
            function() {
                var item = $(this);
                var href = item.attr('href');
                runUpdate(href, item);
            },
            function() {
                var item = $(this);
                var rel = item.attr('rel');
                runUpdate(rel, item);
            }
        );
    });

    function removeAddColorFollow(item) {
        var href = $(this).attr('href');
        var rel = $(this).attr('rel');
        if (item.hasClass('btn-success')) {
            $(item).removeClass('btn-success').attr('href', href).attr('rel', rel);
            $(item).find('i').removeClass('icon-white');
        } else {
            $(item).addClass('btn-success').attr('href', rel).attr('rel', href);
            $(item).find('i').addClass('icon-white');
        };
    }
});

Upvotes: 1

Views: 156

Answers (2)

Imdad
Imdad

Reputation: 6042

The code $('.mini-like').live('click',... should be placed inside $(document).ready()

You can use .on in place of .live. As .on is a new method and .live is deprecated now you should use .on

UPDATE The re-written version will be

$(document).ready(function(){
 $('.mini-like').on('click', function(){
    $('.mini-like').toggle(
        function() {
            var item = $(this);
            var href = item.attr('href');
            runUpdate(href, item);
        },
        function() {
            var item = $(this);
            var rel = item.attr('rel');
            runUpdate(rel, item);
        }
    );
 });

});

function runUpdate(url, item) {
    $.ajax({
        type: "GET",
        url: url,
        cache: false,
        success: function(data){
            if (data == '200') {
                removeAddColor(item);
            }
        }
    });
}


function removeAddColorFollow(item) {
    var href = $(this).attr('href');
    var rel = $(this).attr('rel');
    if (item.hasClass('btn-success')) {
        $(item).removeClass('btn-success').attr('href', href).attr('rel', rel);
        $(item).find('i').removeClass('icon-white');
    } else {
        $(item).addClass('btn-success').attr('href', rel).attr('rel', href);
        $(item).find('i').addClass('icon-white');
    };
}

Upvotes: 0

Hubro
Hubro

Reputation: 59388

Well unless I'm completely wrong, you only attach the toggle event to .mini-like after it has been clicked once. Try to just replace

$('.mini-like').live('click', function() {...

With

$(function() {...

To attach the toggle event handler on document ready instead of on click

Upvotes: 1

Related Questions