Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- document.getElementsByClassName( "myclass" ).forEach( function(element, index, array) {
- //do stuff
- });
- [2, 5, 9].forEach( function(element, index, array) {
- //do stuff
- });
- var els = document.getElementsByClassName("myclass");
- var elsArray = Array.prototype.slice.call(els, 0);
- elsArray.forEach(function(el) {
- // Do stuff with the element
- console.log(el.tagName);
- });
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <script>
- function findTheOddOnes()
- {
- var theOddOnes = document.getElementsByClassName("odd");
- for(var i=0; i<theOddOnes.length; i++)
- {
- alert(theOddOnes[i].innerHTML);
- }
- }
- </script>
- </head>
- <body>
- <h1>getElementsByClassName Test</h1>
- <p class="odd">This is an odd para.</p>
- <p>This is an even para.</p>
- <p class="odd">This one is also odd.</p>
- <p>This one is not odd.</p>
- <form>
- <input type="button" value="Find the odd ones..." onclick="findTheOddOnes()">
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement