Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

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


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


  <h2>Event Delegation to Avoid Loops</h2>
  <p>This demo adds a click event to each cell in the table. This can be done using a loop, but instead it's done using event delegation. The event is attached only to the parent <code>&lt;table&gt;</code> element, then the <code></code> property is used to determine where the event actually ocurred.</p>

  <table id="myTable">
    <caption>Click Any Cell in the Table</caption>
      <td>Cell 1</td>
      <td>Cell 2</td>
      <td>Cell 3</td>
      <td>Cell 4</td>
      <td>Cell 5</td>
      <td>Cell 6</td>
      <td>Cell 7</td>
      <td>Cell 8</td>
      <td>Cell 9</td>
      <td>Cell 10</td>
      <td>Cell 11</td>
      <td>Cell 12</td>
      <td>Cell 13</td>
      <td>Cell 14</td>
      <td>Cell 15</td>
      <td>Cell 16</td>
      <td>Cell 17</td>
      <td>Cell 18</td>
      <td>Cell 19</td>
      <td>Cell 20</td>
      <td>Cell 21</td>
      <td>Cell 22</td>
      <td>Cell 23</td>
      <td>Cell 24</td>
      <td>Cell 25</td>



                body {
  font-family: Arial, sans-serif;
  font-size: 20px;
  padding: 0 20px;

main {
  text-align: center;
  margin: 0 auto;
  max-width: 800px;

p {
  text-align: left;
  padding: 0 20px;

code {
  color: firebrick;

table {
  border-spacing: 0;
  border: solid 2px #aaa;
  width: 100%;

caption {
  font-weight: bold;
  padding: 10px;

td {
  border: solid 2px #aaa;
  padding: 10px;

.tdbg {
  color: white;
  background: hotpink;


                /* To attach a click event to all table cells, you can use a loop, like this:

let cells = document.querySelectorAll('td');

for (i of cells) {
  i.addEventListener('click', function () {
  }, false);

/* By delegating the event to the parent element, you can get the same result with much nicer and more efficient code: */

let table = document.querySelector('table');

table.addEventListener('click', function (e) {
  if ( === 'td') {'tdbg');
}, false);