Posted By

omerta83 on 07/08/10


Tagged

javascript jquery


Versions (?)

Treating jQuery objects like arrays


 / Published in: JavaScript
 

  1. /*Here's some sample HTML followed by some jQuery
  2. that allows us to access the values of any "box"
  3. by index.*/
  4. <div id="wrapper">
  5. <div class="box">Content #1!</div>
  6. <div class="box">Content #2!</div>
  7. <div class="box">Content #3!</div>
  8. <div class="box">Content #4!</div>
  9. </div>
  10. <div id="wrapper2">
  11. <div class="box">Content2 #1!</div>
  12. </div>
  13.  
  14. // returns 4
  15. $('#wrapper .box').length;
  16.  
  17. // num is equal to 4
  18. var num = $('#wrapper .box');
  19. num = num.length;
  20.  
  21. // text is equal to 'Content #2!'
  22. var text = $("#wrapper .box")[1];
  23.  
  24. // text is equal to 'Content #1'
  25. var text = $("#wrapper .box")[0];
  26.  
  27. // text is equal to 'Content2 #1'
  28. var text = $("#wrapper2 .box")[0];
  29.  

Report this snippet  

You need to login to post a comment.