Pen Settings

HTML

CSS

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

JavaScript

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

Packages

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.

Behavior

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.

HTML

              
                .container
  .row
    .col
      .content
        button#reset.btn.btn-danger Reset
  .row
    .col-md-6.col-12
      .content
        #root1
        .text-center
          button.btn.btn-primary#innerHTML(data-count='0') innerHTML
        br
        pre.code update by innerHTML, events are deleted.
        
    .col-md-6.col-12
      .content
        #root2
        .text-center
          button.btn.btn-primary#insertAdjacentHML(data-count='0') insertAdjacentHML
        br
        div.form-group
          each item, i in ['beforebegin', 'afterbegin', 'beforeend', 'afterend']
            label.form-check-inline
              input.radio.form-radio-input(type="radio", name="position", value= item, checked= i === 0)
              = item
  .row
    .col-12
      .content
        #root3
        div
          button.btn.btn-info#addNode(data-count='0') ADD Item
        br
        div.form-group
          each item, i in ['appendChild', 'insertBefore', 'append', 'prepend']
            label.form-check-inline
              input.radio.form-radio-input(type="radio", name="method", value= item, checked= i === 0)
              = item

template#template
  p
    b Count: 
      span.count 0
  ol.list
    li list item
      button.btn.btn-sm.btn-success count up
              
            
!

CSS

              
                body
  background-color: #efefef;

.content
  padding: 1em;
  background-color: #fff;

.row + .row
  margin-top: 1em;

.code
  font-family: monospace;
  padding: .5em;
  background-color: #eee;
  border-radius: .2em;

.radio
  margin-right: .25em;

 .addItem
   background: linear-gradient(transparent 60%, #feee99 60%);
              
            
!

JS

              
                const d = document;

const getTemplateContent = (id) => {
  const t = d.querySelector(id);
  return d.importNode(t.content, true);
};

const onReset = () => {
  console.log('onReset');
  const areas = ['#root1', '#root2', '#root3'];
  areas.forEach((id) => {
    const area = d.querySelector(id);
    const t = getTemplateContent('#template');
    const countBtn = t.querySelector('.btn');
    countBtn.addEventListener('click', (e) => {
      const wrapper = e.currentTarget.parentNode.parentNode.parentNode;
      const counter = wrapper.querySelector('.count');
      counter.textContent = Number(counter.textContent) + 1;
    });
    area.innerHTML = '';
    area.append(t);
  });
};

const getAreaListElement = (areaID) => {
  const area = d.querySelector(areaID);
  return area.querySelector('.list');
}

const onInsertContentByInnerHTML = (areaID, count) => {
  const list = getAreaListElement(areaID);
  list.innerHTML += `<li class="addItem">Add Item ${count}</li>`;
};

const onInsertContentByInsertAdjacentHTML = (areaID, count) => {
  const list = getAreaListElement(areaID);
  const checkedRadio = d.querySelector(areaID).parentNode.querySelector('input[type="radio"]:checked');
  const position = checkedRadio.value;
  const tag = position === 'beforebegin' || position === 'afterend' ? 'div' : 'li';
  list.insertAdjacentHTML(position, `<${tag} class="addItem">Add item - ${position} ${count}</${tag}>`);
};

const onInsertContentByNode = (areaID, count) => {
  const list = getAreaListElement(areaID);
  const checkedRadio = d.querySelector(areaID).parentNode.querySelector('input[type="radio"]:checked');
  const addContent = `Add item - ${checkedRadio.value} ${count}`;
  const addElement = d.createElement('li');
  addElement.className = 'addItem';
  addElement.textContent = addContent;
  console.log(checkedRadio.value);
  switch(checkedRadio.value) {
    case 'appendChild': {
      return list.appendChild(addElement);
    }
    case 'insertBefore': {
      const firstChild = list.firstChild
      return list.insertBefore(addElement, firstChild);
    }
    case 'append': {
      list.append(addElement);
      return list.lastChild.append('<b>append by String</b>');
    }
    case 'prepend': {
      list.prepend(addElement);
      return list.firstChild.prepend('<b>prepend by String</b>');
    }
  }
}

!function() {
  console.log('----');
  d.querySelector('#reset').addEventListener('click', onReset);
  d.querySelector('#innerHTML').addEventListener('click', (e) => {
    const btn = e.currentTarget;
    const count = btn.dataset.count - 0;
    btn.dataset.count = count + 1;
    onInsertContentByInnerHTML('#root1', count);
  });
  d.querySelector('#insertAdjacentHML').addEventListener('click', (e) => {
    const btn = e.currentTarget;
    const count = btn.dataset.count - 0;
    btn.dataset.count = count + 1;
    onInsertContentByInsertAdjacentHTML('#root2', count);
  });
  d.querySelector('#addNode').addEventListener('click', (e) => {
    const btn = e.currentTarget;
    const count = btn.dataset.count - 0;
    btn.dataset.count = count + 1;
    onInsertContentByNode('#root3', count);
  });
  onReset();
}();

              
            
!
999px

Console