Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <form id="myform" action="get">
  <fieldset>
    <legend>Example form</legend>
    
    <div class="formgrid">
      
      <input id="name" name="name" type="text" />
      <label for="name">name</label>
      
      <input id="email" name="email" type="email" />
      <label for="email">email</label>
      
      <textarea id="comments" name="comments" rows="5" cols="20"></textarea>
      <label for="comments">comments</label>
      
      <button type="submit">SUBMIT</button>
      
    </div>
    
  </fieldset>
</form>
              
            
!

CSS

              
                * {
  font-family: sans-serif;
  font-size: 1em;
}

body {
  color: #111;
  background-color: #eee;
}

body, input, textarea, select, option, button, label {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Droid Sans", "Helvetica Neue", sans-serif;
  font-size: 100%;
}

p {
  max-width: 40em;
  margin: 1em auto;
}

fieldset {
  max-width: 40em;
  padding: 4px;
  margin: 2em auto;
  border: 0 none;
}

legend {
  font-size: 1.2em;
  width: 100%;
  margin-bottom: 0.5em;
  border-bottom: 1px dotted #99c;
}

input, output, textarea, select, button {
  box-sizing: border-box;
  padding: 0.2em 0.4em;
  margin: 0.2em 0;
  outline: 0 none;
  border: 0 none;
  box-shadow: none;
}

button {
  max-width: 9em;
  padding: 0.2em 2em;
  background-color: #ddd;
  box-shadow: 0 2px 0 #bbb;
  cursor: pointer;
}

label {
  width: auto;
  text-align: right;
  box-sizing: border-box;
  user-select: none;
  cursor: pointer;
}

input:focus + label, textarea:focus + label, select:focus + label {
  font-weight: bold;
}

.formgrid {
  display: grid;
  grid-template-columns: 1fr 1em 2fr;
  grid-gap: 0.3em 0.6em;
  grid-auto-flow: dense;
  align-items: center;
}

input, output, textarea, select, button {
  grid-column: 2 / 4;
  width: auto;
  margin: 0;
}

textarea + label {
  align-self: start;
}
              
            
!

JS

              
                // form binding class
class FormBind {
  
  #Field = {};
  
  constructor(form) {
    
    // initialize object properties
    const elements = form.elements;
    for (let f = 1; f < elements.length; f++) {
      
      const field = elements[f], name = field.name;
      if (name) {
        this[name] = field.value;
        this.#Field[name] = field;
      }
      
    }

    // form change events
    form.addEventListener('input', e => {
      
      const name = e.target.name;
      if (this.#Field[ name ]) {
        this[ name ] = this.#Field[ name ].value;
      }
      
    });
    
  }
  
  
  // update property and field
  updateValue(property, newValue) {
    
    if (this.#Field[ property ]) {
      
      this[property] = newValue;
      this.#Field[property].value = newValue;
      return true;
      
    }
    
    return false;
    
  }
  
}


// form proxy traps
const FormProxy = {

  // intercept set
  set(target, property, newValue) {
    return target.updateValue(property, newValue);
  }
  
};


// form 2-way data binder
function FormBinder(form) {
  return form ? new Proxy(new FormBind(form), FormProxy) : undefined;
}



// form reference
const myformElement = document.getElementById('myform');

// prevent submit and show current properties
myformElement.addEventListener('submit', e => {
  e.preventDefault();
  showProperties(myForm);
});


// create 2-way data form
const myForm = FormBinder(myformElement);

myForm.name = "Some Name";
myForm.email = 'test@email.com';
myForm.comments = 'no comment';

// show form properties
function showProperties(obj) {
  
  console.clear();
  console.log('Current values:');
  for (const p in obj) {
    console.log(`${ p }: ${ obj[ p ] }`)
  }
  
}
              
            
!
999px

Console