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.

            
              <section>
  <ul id="parent-ul-1">
    <li id="li-1" class="my-class">Item 1</li>
    <li id="li-2" class="my-class">Item 2</li>
    <li id="li-3" class="my-class">Item 3</li>
  </ul>
  <p id="display-msg-1"><p>
</section>

<section>
  <ul id="parent-ul-2">
    <li id="li-4" class="my-class">Item 4</li>
    <li id="li-5" class="my-class">Item 5</li>
    <li id="li-6" class="my-class">Item 6</li>
  </ul>
  <p id="display-msg-2"><p>
</section>

<section>
  <ul id="parent-ul-3">
    <li id="li-7" class="my-class">Item 7</li>
    <li id="li-8" class="my-class">Item 8</li>
    <li id="li-9" class="my-class">Item 9</li>
  </ul>
  <p id="display-msg-3"><p>
</section>

<section>
  <button id="unbind" type="button">Unbind Column 3</button>
  <button id="bind" type="button">Bind Column 3</button>
  <p id="display-msg-4"><p>
</section>
            
          
!
            
              $pri: #888;
$pri-light: lighten($pri, 20%);
$pri-dark: darken($pri, 20%);
$pri-dark-x: darken($pri, 30%);
* { box-sizing: border-box; }
body {
  font-family: arial;
  margin: 2em;
  background: #222;
}
@keyframes play {
   0% { left: 0; }
   50% { left: 20px; }
   100% { left: 0;}
}
.moveMe {
  animation: play .8s;
  position: relative;
  margin: 0;
}
section {
  width: 25%;
  float: left;
  padding-right: 1%;
  }
ul {
  width: 100%;
  color: $pri-light;
  li {
    background-color: $pri-dark-x;
    border-bottom: 2px solid $pri-light;
    padding: 1em;
    list-style-type: none;
    cursor: pointer;
  }
 }
p {
  padding: 1em 0 0 0;
  color: #fff;
  font-size: 1em;
}
button {
  width: 100%;
  display: block;
  border: 0;
  background-color: $pri-dark;
  padding: 1em;
  margin-bottom: 1em;
  font-size: 1em;
  color: $pri-light;
  border: 5px solid  $pri-light;
  &:hover { background-color: $pri-dark-x; }
  &:active { 
    box-shadow: inset 0 0 5px $pri-dark-x;
    background-color: $pri-dark-x; }
  &:focus { outline: 0; }
}
            
          
!
            
              
// Event delegation is useful in that you only place the event listener on the parent to capture the bubbled event - imagine a table of 1000 rows. The callback function lets you handle it in any way.

var dm1 = document.getElementById("display-msg-1");
document.getElementById("parent-ul-1").addEventListener("click", function(e) {
  if(e.target) {
    var msg = "Clicked on: " + e.target.firstChild.nodeValue;
    // You could capture any attribute of this clicked element
    // "ID:" + e.target.id or "Class:" + e.target.className
    addRemoveClass(dm1, msg);
  }
});
function addRemoveClass(c, msg) {
  c.innerHTML = msg;
  c.classList.remove("moveMe");
  c.offsetWidth = c.offsetWidth;
  c.classList.add("moveMe");
}
// And with jQuery .delegate
$("#parent-ul-2").delegate( "li", "click", function() {
  var dm2 = $("#display-msg-2");
  var msg = dm2.text( "Clicked on: " + $(this).text());
  jq_addRemoveClass(dm2, msg);
});
// For.on just reverse the event and target
$("#parent-ul-3").on("click", "li", function() {
  var dm3 = $("#display-msg-3");
  var msg = dm3.text( "Clicked on: " + $(this).text());
  jq_addRemoveClass(dm3, msg);
});
function jq_addRemoveClass(c, msg) {
  c.removeClass("moveMe");
  c.outerWidth(true);
  c.addClass("moveMe");
}
// .off - removes both .delegate and .on
$( "#unbind" ).click(function() {
  $( "#parent-ul-3" )
    .off( "click", "li")
    .find( "#display-msg-4" )
    $("#display-msg-4" ).text("Removed .on from C3");
});
$("#bind").click(function() {
  // Real world, I'd abstract the sect 3 declaration into it's own function
    $("#parent-ul-3").on("click", "li", function() {
      
      var dm3 = $("#display-msg-3");
      var msg = dm3.text( "Clicked on: " + $(this).text());
      jq_addRemoveClass(dm3, msg);
    });
  $("#display-msg-4").text("Added .on to C3");
})
            
          
!
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