Posted By

pedromagalhaes on 02/23/11


Tagged

columns


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

maltem


Style elements, give classes, clear floats and add separators (for rows and columns)


 / Published in: jQuery
 

Call the function like this:

getChild('.div ul li',4,'newclass',1,1);

Explain: Get all the li's, on every 4 add "newclass", plus, i want a ".clear" and ".seperator" div (to clear the floats and style the separator properly)

  1. $(document).ready(function(){
  2. function getChild($class,$pos,$add,$clear,$separator){
  3. $num_divs = $($class).length;
  4. $i = 1;
  5. for($i; $i <= $num_divs; $i++){
  6. if($i % $pos == 0){
  7. $($class).eq($i-1).addClass($add);
  8. if($clear == 1){
  9. $('<div class="clear"></div>').insertAfter($($class).eq($i-1));
  10. }
  11. if($separator == 1){
  12. $('<div class="separator"></div>').insertAfter($($class).eq($i-1));
  13. }
  14. }
  15. }
  16. }
  17. getChild('.div ul li',4,'newclass',1,1);
  18. });

Report this snippet  

You need to login to post a comment.