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


/ Published in: jQuery
Save to your folder(s)

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)


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.