jquery - Adding class when .next() reaches end of .length()? -


im there jquery not adding class @ right time, there 2 clicks.

check out fiddle: http://jsfiddle.net/x7l8q/4/

jquery:

  var currentitem = $('.item').filter('.active');    $('#next-button').on('click', function () {       var nextitem = currentitem.next();       currentitem.removeclass('active');        //here meat of code, adding class few clicks later want.       if (nextitem.length) {           currentitem = nextitem.addclass('active');       } else if (nextitem.length == 0) {           $('.item:last').addclass('red');           alert('class red added');       }    }); 

html:

the jquery should add class last item when #next-button clicked add .active .next() item, if nextitem == 0 add class red problem happens 2 clicks late.

check fiddle see mean.

  <div class="item active" data-category="sharks">content1</div>   <div class="item" data-category="tigers">content2</div>   <div class="item" data-category="lions">content3</div>    <a href="#" class="btn btn-primary" id="next-button" rel="nofollow">next</a> 

wow these answers good! guys!

try

var items = $('.item'), currentitem = items.filter('.active'), last = items.last();  $('#next-button').on('click', function () {     currentitem.removeclass('active');     var nextitem = currentitem.next();      if (nextitem.length) {         currentitem = nextitem.addclass('active');         if (currentitem.is(last)) {             $('.item:last').addclass('red');         }     } }); 

demo: fiddle


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -