javascript - Finding the next element divisible by 4 -


so have list of elements so;

<div></div> <div class="test"></div> <div></div> <div class="test2"></div> <div></div> <div></div> <div class="test3"></div> <div></div> <div class="test4"></div> <div></div> 

and need figure out next element relative selector order divisible four, counting beginning. if there aren't enough elements relative selector, last element returned. ergo, i'd following sort of results;

$(".test").nextfour().after("hello"); // or $(".test2").nextfour().after("hello");  <div></div> <div class="test"></div> <div></div> <div class="test2"></div> hello <div></div> <div></div> <div class="test3"></div> <div></div> <div class="test4"></div> <div></div> 

$(".test3").nextfour().addclass("hello");  <div></div> <div class="test"></div> <div></div> <div class="test2"></div> <div></div> <div></div> <div class="test3"></div> <div class="hello"></div> <div class="test4"></div> <div></div> 

$(".test4").nextfour().css("color", "red");  <div></div> <div class="test"></div> <div></div> <div class="test2"></div> <div></div> <div></div> <div class="test3"></div> <div></div> <div class="test4"></div> <div style="color: red;"></div> 

how achieve this?

var elem = $(selector); var idx = elem.index() + 1;  var target = elem.parent().children().eq(idx + 4-(idx%4)-1);  if (!target.length)     target = elem.parent().children().last(); 

demo: http://jsfiddle.net/zzvax/1/


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -