Select/Traverse based on Length of Elements


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

Useful if you want to manipulate/style/remove items after a given index count.


Copy this code and paste it in your HTML
  1. //HTML
  2. <div>Index 0</div>
  3. <div>Index 1</div>
  4. <div>Index 2</div>
  5. <div>Index 3</div>
  6. <div>Index 4</div>
  7.  
  8.  
  9. //SCRIPT
  10. $(document).ready(function(){
  11.  
  12. //get the number of DIV's
  13. var divCount = $('div').length;
  14.  
  15. //check to see if the count is greater than 3
  16. if (divCount > 3) {
  17.  
  18. //count starts at zero (0, 1, 2...)
  19. $('div:gt(2)').css('background','darkOrange');
  20.  
  21. }
  22.  
  23. });//jQuery

URL: http://marioluevanos.com/playground/Select%20Lenght%20of%20Elements/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.