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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <h2>Document and Button Clicking</h2>
<p>Click this document to activate the handler.</p>
<div id="h1"></div>
<div id="h2"></div>
<button>Click me</button>
<button>Click me too</button>
<button>One more click</button>
<h2>Playing with Style Sheets</h2>
<p>This page turns violet when you hold the V key.</p>
<h2>MouseDown and MouseMove</h2>
<p>Drag the bar to change its width:</p>
<div id="bar">
</div>
<h2>Form Focus Events</h2>
<form>
<p>Name: <input type="text" data-help="Your full name"></p>
<p>Age: <input type="text" data-help="Your age in years"></p>
<p id="help"></p>
</form>
              
            
!

CSS

              
                body {
  font-size: 1.5em;
  font-family: Arial;
}
#bar {
  background: red; 
  width: 60px; 
  height: 20px;
}


              
            
!

JS

              
                // Mouse click event that will record when anyone clicks on anything within the document - Remember to open the Console to see the responses

// TO DO: Add a heading (h1) and another heading (h2) to the HTML section and see if the click event works for those

var flag = false;
window.addEventListener("click", () => {
    console.log("You clicked?");
  if(flag == false) {
  var div = document.getElementById("h1");
  div.innerHTML += "<h1>H1 Added!</h1>";
  div = document.getElementById("h2");
  div.innerHTML += "<h2>H2 Added!</h2>";
    flag = true;
  }
});

// This next example is a mouse click event that will trigger when someone clicks on the FIRST button on the page. What tells us that it will only work on the FIRST button?

let button = document.querySelector("button");
button.addEventListener("click", () => {
  console.log("First Button clicked.");
});

// How can we modify this so that it will occur when the 2nd  button is clicked?
// We need to use querySelectorAll which will produce a nodelist/array of all the buttons. Then we can reference which button we want to apply the click event using [] with the array element value - in this case 1
let second_button = document.querySelectorAll("button");
second_button[1].addEventListener("click", () => {
  console.log("Second Button clicked.");
});
// TO DO -  How can we modify this so that the events will occur when ANY button is clicked? Add a third button and create some code that will assign click events with the message "Any button clicked" in response to any button clicks. Think about it. 

let any_button = document.querySelectorAll("button");
for(var i = 0; i<any_button.length; i++) {
any_button[i].addEventListener("click", () => {
  console.log("Any button clicked");
});
}

// Let's explore a keypress event that triggers a change in the DOM. Think of this like applying a "theme" to our webpage design.  In this first example, when someone presses the p button, the style changes to a purple background and white text.

window.addEventListener("keydown", event => {
if (event.key == "p") {
  document.body.style.background = "purple";
  document.body.style.color = "white";
}
});

// What if we only wanted the keypress to happen while the key is pressed, and to go back to normal once the key is no longer being pressed? We can remove the event listener like this:

window.addEventListener("keydown", event => {
if (event.key == "v") {
  document.body.style.background = "violet";
}
});
window.addEventListener("keyup", event => {
if (event.key == "v") {
  document.body.style.background = "";
  document.body.style.color = "";
}
});

// This is a really fun example of how usingmousedown/mousemove events can allow a user to interact with an object on your webpage. One thing to note however is that the mouse events might not work on touchscreens. 

let lastX; // Tracks the last observed mouse X position
let bar = document.getElementById("bar");
bar.addEventListener("mousedown", event => {
if (event.button == 0) {
  lastX = event.clientX;
  window.addEventListener("mousemove", moved);
  event.preventDefault(); // Prevent selection
  }
});
function moved(event) {
  if (event.buttons == 0) {
    window.removeEventListener("mousemove", moved);
  } 
  else {
    let dist = event.clientX - lastX;
    let newWidth = Math.max(10, bar.offsetWidth + dist);
    bar.style.width = newWidth + "px";
    lastX = event.clientX;
    }
}
// This is an example using a form. Form validation is a huge topic in web development and will need to be fully understood by students. Take a look at this example using the focus event on particular form elements. The data-help text is being pulled by the html attribute of the form element and that is what is outputting to the document
let help = document.querySelector("#help");
let fields = document.querySelectorAll("input");
for (let field of Array.from(fields)) {
  field.addEventListener("focus", event => {
  let text = event.target.getAttribute("data-help");
  help.textContent = text;
  });
field.addEventListener("blur", event => {
help.textContent = "";
});
}
              
            
!
999px

Console