jeudi 23 avril 2015

jQuery. Pass a variable from a function to an .each inside

I want to pass a variable, either '1' or '2' and run a function depending on available size. How can i ensure that the var can get pass through another function? Thanks!

$(function() {
    function newFunc(varNum){
        $('.listing__item .item__details').each(function() {
            var ul = $('ul.item__authors', this);
            var authors = $('.item__authors-info', this);
            if(ul.children('li.author').size() <= varNum) return;

            var hiddenElements = ul.children('li:gt(varNum)', this).hide(),
                showCaption = '+ ' + hiddenElements.size() + ' others';

            authors.append($('<span class="other-authors">' + showCaption + '</span>'));
        });
    }       
    newFunc('1');
});

Aucun commentaire:

Enregistrer un commentaire