cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <h2>Click the Lights</h2>

<div onclick="switchLight(0)" class="light stop on" ></div>
<div onclick="switchLight(1)" class="light slow" ></div>
<div onclick="switchLight(2)" class="light go" ></div>



















<p>
  Click on the lights and then look at the HTML. Notice the attribute I've added to each div. Then look at the JavaScript (JS) to see what's going on.
</p>
            
          
!
            
              .light {
  height: 120px;
  width : 120px;
  border-radius: 50%;
  opacity : 0.3; /* makes colors less bright by default */
  margin: 0 auto;
  display: inline-block;
}

.stop {background-color: red;}
.slow {background-color: yellow;}
.go {background-color: green;}
.on {opacity: 1;}

body {
  background-color : black;
  text-align: center;
  color :white;
  font-family: Helvetica;
}
            
          
!
            
              /* This line gets all the HTML elements with class="light" and assigns them to a variable called LIGHTS. */
var LIGHTS = document.getElementsByClassName("light");

/* This defines a function which turns off every light by removing the "on" class from the HTML elements.*/
function allOff() {
  for (var i=0; i<LIGHTS.length; i = i+1) {
    LIGHTS[i].classList.remove("on");
  }
}

/* This defines a function which first turns off every light and then immediately turns one on.*/
function switchLight(lightNumber) {
  allOff();
  var light = LIGHTS[lightNumber];
  light.classList.add("on");
}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console