Using .slice() method to style only particular elements within a list


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



Copy this code and paste it in your HTML
  1. /*
  2. <ul>
  3. <li>Apples</li>
  4. <li>Pears</li>
  5. <li>Cherries</li>
  6. <li>Grapes</li>
  7. <li>Mangos</li>
  8. </ul>
  9. */
  10.  
  11. /*If we just want to set the background-color of
  12.   everything after element number two (pears) we
  13.   can do this using:*/
  14.  
  15. $('li').slice(2).css('background-color', 'yellow');
  16.  
  17. /*and if we want to set the bg-color of the elements
  18.   after two(pears), but only up to and including 4
  19.   (grapes), we can use:*/
  20. $('li').slice(2, 4).css('background-color', 'green')
  21.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.