SV: [thelist] JS add remove list items

Marcus Andersson marcan at home.se
Sat Nov 15 11:46:24 CST 2003


I would do something like the following (explanations in comments). Hope
it helps.

/Marcus

/*
* This functions finds out the last bullet that is visible
*/
function getLastVisible(bullets) {
  for(var i = 0 ; i < bullets.length ; i++) {
    if(bullets[i].style.display == "none") {          
      return i - 1;
    }
  }
  // all is visible
  return bullets.length - 1;
}

/*
* Shows or hides bullets in the list depending on the action
*/
function bullet(action) {
  // get all bullets
  var bullets = document.getElementsByTagName("li"); 
  // get the index of the last visible bullet
  var lastVisible = getLastVisible(bullets);
  if (action == "add") {
    // Check to see that all bullets aren't visible
    if((lastVisible + 1) < bullets.length) {
      // Set display on the first non visible bullet.
      // That is the same as the last visible + 1
      bullets[lastVisible + 1].style.display="block";           
      return;
    }
  }
  else {
    // Check to see that all bullets aren't hidden
    if(lastVisible >= 0) {
      // Hide the last visible bullet
      bullets[lastVisible].style.display="none";           
      return;
    }
  }
}



More information about the thelist mailing list