css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="item item-1">I am an item.</div>
<div class="item item-2">I am an item.</div>
<div class="item item-3">I am an item.</div>
<div class="item item-4">I am an item.</div>
<div class="item item-5">I am an item.</div>
            
          
!
            
              body {
  display: flex;
}

.item {
  padding: 1rem;
  margin: 1rem 0 1rem 1rem;
  border: 1px solid #ccc;
  max-width: 10rem;
  box-shadow: 2px 5px 20px rgba(black, 0.07);
  border-radius: 0.2rem;
}
            
          
!
            
              /*
METHOD ONE
*/

// first select all of the items..
const items = document.getElementsByClassName('item');
// At this point, items = a HTMLCollection with 5 items
// HTMLCollection(5) [div.item,div.item,div.item,div.item,div.item ]

// Using the new spread operator, we can transform this HTMLCollection
// into an array, which gives us access to a bunch of useful methods
const itemsArray = [...items];
// itemsArray now = a array(5) [div.item, div.item, div.item, div.item, div.item]

// now we have an array we can easily use over them using forEach
itemsArray.forEach((item, index) => {
  console.log(
    // each of items is an 'Element', which gives you access to a bunch
    // of object methods https://developer.mozilla.org/en-US/docs/Web/API/Element
    item,
    // also, you can access the index of the loop
    index
  );
});

/*
METHOD TWO
but we can make this one step shorter!
Also if you don't need the index, feel free to omit it
note we already defined const items above
*/
[...items].forEach(item => console.log(item));

/*
METHOD THREE
or if you were feeling really wild...
this is potentially less performant, if there's any chance
you'll be running this loop more than once, you'll definately
want to store the HTMLCollection in memory first, but that's up to you
*/
[...document.getElementsByClassName('item')].forEach(item => console.log(item));

/*
BONUS ROUND
*/
Array.from(items).forEach(item => console.log(item));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console