Posted By

neogeek on 07/01/06


Tagged

css style strip elements


Versions (?)

Who likes this?

14 people have marked this snippet as a favorite

luman
arcturus
luxuryluke
splorp
jonbaer
soxiam
trickychicken
buscarini
SpinZ
esquareda
BFTrick
seanpowell
guo3823538
wirenaught


Stripe Elements


 / Published in: JavaScript
 

This script will allow you to alternately place a style on objects (in this example, every other list item).

  1. function stripeElement(objects, className) {
  2.  
  3. for (var i = 0; i < objects.length; i++) {
  4. if (i % 2) { objects[i].className += ' ' + className; }
  5. }
  6.  
  7. }
  8.  
  9. window.onload = function() {
  10.  
  11. if (document.getElementById('list')) {
  12. js_lib.css.mod.stripeElement(document.getElementById('list').getElementsByTagName('li'), 'tint');
  13. }
  14.  
  15. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Roshambo on July 27, 2006

Your for loop could probably be better written as:

for(var i = 0; i < objects.length; i = i + 2) {
    objects[i].className += ' ' + className;
}

That way you wouldn't have to nest an "if" condition in the loop.

You need to login to post a comment.