Pen Settings

HTML

CSS

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

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

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.

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

              
                <div class="container">
<div>
  <div class="button addBtn">Add Child Node</div>
  <div class="button removeBtn">Remove Child Node</div>
    <div class="button changeAttrBtn">Change Attribute Value</div>
</div>
<div class="parent" data-count="0">
  <div class="child"> Child </div>
</div>
  <div class="infoBoard">
    
  </div>
</div>
              
            
!

CSS

              
                .container {
  margin: 10px;
}

.button {
    background: purple;
    color: white;
    cursor: pointer;
    display: inline-block;
    font-size: 20px;
    padding: 1.75rem 1rem;
    text-align: center;
    text-decoration: none;
  font-weight: bold;
  margin: 5px;
}

.button:hover {
  background: #AD1457;  
}

.parent {
  border: 1px solid #ccc;
  padding: 10px;
}

.child {
  background: #673AB7;
  color: #fff;
  padding: 10px;
  margin-top: 5px;
}

.infoBoard {
  padding: 10px;
  border: 1px solid #ccc;
  font-size: 20px;
  padding: 1.75rem 1rem;
  background: #f4f4f4;
}
              
            
!

JS

              
                if(!("MutationObserver" in window)) {
  document.body.innerText = "Not supported by your browser";
}

/* Create mutation observer */
const config = {
  childList: true,
  attributes: true,
  characterData: true
};
const observer = new MutationObserver(handler);

/* Observe target */
const parent = document.querySelector(".parent");
observer.observe(parent, config);

/* Callback function when mutation happens */
function handler(mutationRecords, observer) {
  console.log("Handle mutation");
  mutationRecords.forEach((mutationRecord) => {
         const info = document.querySelector(".infoBoard");
      switch(mutationRecord.type) {
        case "childList":
info.innerText = "Mutation Observer handler: Child node added or removed";
          
          break;
        case "attributes":
          info.innerText = `Mutation Observer handler: Parent attribute modified: ${mutationRecord.attributeName} : ${mutationRecord.target.children.length}`;
          break;
        default:
          info.innerText = "";
      }
  });
}

/* Add child nodes when clicked on add child node button */

document.querySelector(".addBtn").addEventListener("click", () => {
console.log("Add child");
  const child = document.createElement("div");
child.className = "child";
  child.innerText = "Child ";
  parent.appendChild(child);
});


/* Remove child nodes when clicked on remove child node button */
document.querySelector(".removeBtn").addEventListener("click", () => {
console.log("Remove child");
  const child = parent.firstChild;
  parent.removeChild(child);
});

/* Change attribute value when clicked on change attribute button */
document.querySelector(".changeAttrBtn").addEventListener("click", () => {
console.log("Change attribute value");
  parent.setAttribute("data-count", parent.childNodes.length);
});
              
            
!
999px

Console