Advertisement
Guest User

Untitled

a guest
Aug 25th, 2013
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. document.getElementsByClassName( "myclass" ).forEach( function(element, index, array) {
  2. //do stuff
  3. });
  4.  
  5. [2, 5, 9].forEach( function(element, index, array) {
  6. //do stuff
  7. });
  8.  
  9. var els = document.getElementsByClassName("myclass");
  10. var elsArray = Array.prototype.slice.call(els, 0);
  11. elsArray.forEach(function(el) {
  12. // Do stuff with the element
  13. console.log(el.tagName);
  14. });
  15.  
  16. <!DOCTYPE html>
  17. <html>
  18. <head>
  19. <meta charset="UTF-8">
  20. <script>
  21. function findTheOddOnes()
  22. {
  23. var theOddOnes = document.getElementsByClassName("odd");
  24. for(var i=0; i<theOddOnes.length; i++)
  25. {
  26. alert(theOddOnes[i].innerHTML);
  27. }
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <h1>getElementsByClassName Test</h1>
  33. <p class="odd">This is an odd para.</p>
  34. <p>This is an even para.</p>
  35. <p class="odd">This one is also odd.</p>
  36. <p>This one is not odd.</p>
  37. <form>
  38. <input type="button" value="Find the odd ones..." onclick="findTheOddOnes()">
  39. </form>
  40. </body>
  41. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement