Sunday 15 September 2013

jQuery, count number of elements and then apply number of another jQuery function -



jQuery, count number of elements and then apply number of another jQuery function -

i'm using little snippet first count number of .item-wrap elements , apply number function fades out button after x number of clicks;

var count_elements = $('.showcase-grid .item-wrap').length; var countclick = 0; $("#load-items").on("click", function() { countclick = countclick + 1; if (countclick >= count_elements) {$("#load-items").fadeout(3000); } });

but, whatever reason, doesn't seem work. if replace count_elements static number function works intended assume i'm doing wrong in initial variable?

one thing should add, .items-wrap elements created via jquery in "slice" function. code here;

var divs = $(".showcase-grid > .item"); for(var = 0; < divs.length; i+=3) { divs.slice(i, i+3).wrapall("<div class='item-wrap row hide'></div>"); }

could cause of woes?

if .item-wrap elements aren't loaded dom when seek , count them error, seek wrapping initialisation in document.ready so:

$(document).ready(function() { var count_elements = $('.showcase-grid .item-wrap').length; ]); edit:

you may want seek document.load so:

$(document).load(function() { var count_elements = $('.showcase-grid .item-wrap').length; ]);

jquery

No comments:

Post a Comment