cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

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.

            
              // This version uses React, a plain HTML version is here: 
// http://codepen.io/dzello/pen/LxwPQJ

/*
Form One looks innocent but it has a serious problem. The enter key will trigger the onClick handler of the "Cancel" button, and onSubmit will never be called because of event.preventDefault() in the onClick handler. So the enter key will act as a cancel, not a save. If you were to remove or forget to add event.preventDefault() in the onClick handler, the enter key would perform BOTH a cancel and a save. Not good.
*/
var FormOne = React.createClass({
  onSave: function(event) {
    event.preventDefault();
    console.log("onSave handler called");
  },
  onCancel: function(event) {
   event.preventDefault();
   console.log("onCancel handler called");
  },
  render: function() {
    return (
      <form onSubmit={this.onSave}>
        <input type="text" />
        <button onClick={this.onCancel}>Cancel</button>
        <button type="submit">Save</button>
      </form>
    );
  }
});

/*
In FormTwo, hitting the enter key inside of a form input will trigger the onSave handler of the form. Most likely, this is the desired behavior. The only difference between FormOne and FormTwo is that the Cancel button is now type=button instead of type=submit.
*/
var FormTwo = React.createClass({
  onSave: function(event) {
    event.preventDefault();
    console.log("onSave handler called");
  },
  onCancel: function(event) {
   event.preventDefault();
   console.log("onCancel handler called");
  },
  render: function() {
    return (
      <form onSubmit={this.onSave}>
        <input type="text" />
        <button type="button" onClick={this.onCancel}>Cancel</button>
        <button type="submit">Save</button>
      </form>
    );
  }
});

ReactDOM.render(
  <div>
    <h3>Enter key submission cancels</h3>
    <FormOne />
    <h3>Enter key submission saves</h3>
    <FormTwo />
    <span><i>open the console to see the events being logged</i></span>
  </div>, document.body);
            
          
!
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