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 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

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

              
                <form class="form contact-form" action="https://formspree.io/donnyburnside1@gmail.com">
  <div>
    <label class="form-label" for="name">Name</label>
    <input class="form-control" id="name" name="name" type="text" required />
  </div>
  <div>
    <label class="form-label" for="email">Email</label>
    <input class="form-control" id="email" name="email" type="email" required />
  </div>
  <div>
    <label class="form-label" for="subject">Subject</label>
    <select class="form-control" id="subject" name="subject" required>
      <option value="" selected disabled>Choose a Subject</option>
      <optgroup label="Option Group">
        <option value="subject-one">Subject One</option>
        <option value="subject-two">Subject Two</option>
        <option value="subject-three">Subject Three</option>
      </optgroup>
      <option value="subject-four">Subject Four</option>
      <option value="subject-five">Subject Five</option>
      <option value="subject-six">Subject Six</option>
    </select>
  </div>
  <div>
    <label class="form-label" for="message">Message</label>
    <textarea class="form-control" id="message" name="message" required></textarea>
  </div>
  <div>
    <label class="form-label" for="subscribe">Subscribe?</label>
    <input class="" id="subscribe" name="subscribe" type="checkbox" />
  </div>
  <div>
    <button class="button" type="submit">Submit</button>
    <button class="button" type="reset">Clear</button>
  </div>
</form>
              
            
!

CSS

              
                /*
  Document
*/

html {
  font-family: sans-serif;
  font-size: 100%;
  line-height: 1.5;
  background-color: #f8f8f8;
  color: #3d3d3d;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  min-height: 100vh;
  font-size: 1rem;
}

/*
  Form
*/

.form {
  & > div {
    & ~ div {
      margin-top: 20px;
    }
    &.is-hidden {
      display: none;
    }
  }
  &-label {
    display: inline-block;
    margin-bottom: 5px;
    font-size: 0.875rem;
    font-weight: 700;
  }
  &-control {
    display: block;
    width: 100%;
    padding: 10px;
    font: 0.875rem/1.5 sans-serif;
    color: #333;
    background-color: #fff;
    border: 1px solid #bbb;
    border-radius: 4px;
    box-sizing: border-box;
  }
}

/*
  Contact Form
*/

.contact-form {
  width: 100%;
  max-width: 568px;
  padding: 40px;
  background-color: #fefefe;
  border-radius: 4px;
  box-shadow: 0px 1px 3px 0 rgba(0, 0, 0, 0.125);
  box-sizing: border-box;
  
  &--loading {
    opacity: 0.5;
    pointer-events: none;
  }
  &--errors {}
  &--sent {
    & > div ~ div {
      margin: 0;
    }
  }
}

/*
  Button
*/

.button {
  appearance: none;
  padding: 5px 10px;
  font: 0.875rem/1.5 sans-serif;
  background-color: #ddd;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  box-sizing: border-box;
}


// $form-border-colour: #c3c3c3;
// $form-border-radius: 5px;
// $form-transition-duration: 0.5s;
// $form-transition-timing: cubic-bezier(0.25, 0.75, 0.25, 1);

// /*
//   Document
// */

// html {
//   font-size: 100%;
//   font-family: sans-serif;
// }

// body {
//   width: 100%;
//   min-height: 100vh;
//   margin: 0;
  
//   display: flex;
//   align-items: center;
//   justify-content: center;
  
//   background-image: radial-gradient(#fff 0%, #eee 100%);
//   color: #333;
// }

// .container {
//   width: 100%;
//   max-width: 720px;
//   padding: 20px;
// }
  
// h1,
// p {
//   text-align: center;
// }

// form {
//   &[data-state] {
//     position: relative;

//     & > * {
//       transition: all $form-transition-duration $form-transition-timing;
//     }
    
//     .sent {
//       position: absolute;
//       top: 50%;
//       left: 50%;
//       transform: translate(-50%, 50%);
//       opacity: 0;
//     }

//   }

//   &[data-state="sending"] {
//     pointer-events: none;
//     user-select: none;
    
//     & > div {
//       opacity: 0.5;
//       transition-duration: 0s;
//     }

//     .sent {
//       opacity: 0;
//     }
//   }

//   &[data-state="sent"] {
//     pointer-events: none;
//     user-select: none;

//     & > * {
//       opacity: 0;
//     }
    
//     .sent {
//       transform: translate(-50%, -50%);
//       opacity: 1;
//     }

//   }

//   &[data-state="error"] {}
  
//   & > div {
//     margin-bottom: 1rem;
//   }
// }
              
            
!

JS

              
                class ContactForm {
  
  constructor(el) {
    this.element = el || null
    this.action = el.action
    this.fields = Object.keys(el.elements).reduce((obj, key) => {
      if (key === el.elements[key].id) obj[key] = el.elements[key]
      return obj
    }, {})
    console.log('Contact Form:', this)
  }
  
  init() {
    this.element.addEventListener('submit', (event) => this.onSubmit(event))
  }
  
  onSubmit(event) {
    event.preventDefault()
    this.element.classList.remove('contact-form--errors')
    this.element.classList.remove('contact-form--sent')
    this.element.classList.add('contact-form--loading')
    this.sendFormData(this.getFormData())
      .then(response => {
        if (response.status === 200) {
          return response.json()
                  .then(res => this.onSent(res))
        } else {
          throw 'An error occured while submitting the form'
        }
      })
      .catch(err => this.onError(err))
  }
  
  onSent(response) {
    for (let i = 0; i < this.element.querySelectorAll('div').length; i++) {
      this.element.querySelectorAll('div')[i].classList.add('is-hidden')
    }
    
    let successElement = document.createElement('div')
    successElement.innerHTML = `Message sent successfully`
    this.element.appendChild(successElement)

    this.element.classList.remove('contact-form--loading')
    this.element.classList.add('contact-form--sent')
    console.log('Sent:', response.success)
  }
  
  onError(err = '') {
    this.element.classList.remove('contact-form--loading')
    this.element.classList.add('contact-form--errors')
    console.error('Error:', err)
  }
  
  sendFormData(data = {}) {
    return new Promise((resolve, reject) => {
      console.log('Send Data:', data)
      fetch(this.action, {
        headers: {
          'Content-Type': 'application/json',
        },
        method: 'POST',
        body: JSON.stringify(data)
      })
      .then(response => resolve(response))
      .catch(err => reject(err))
    })
  }
  
  getFormData() {
    let formData = {},
        values = []

    Object.keys(this.fields).forEach(key => {
      switch(this.fields[key].type) {
        case 'checkbox':
        case 'radio': {
          formData[key] = this.fields[key].checked
          break
        }
        case'select-one': {
          if (!this.fields[key][this.fields[key].selectedIndex].disabled) {
            formData[key] = this.fields[key][this.fields[key].selectedIndex].value
          } else {
            formData[key] = ''
          }
          break
        }
        case'select-multiple': {
          Object.keys(this.fields[key].options).forEach(i => {
            if (this.fields[key].options[i].selected && !this.fields[key].options[i].disabled) {
              values.push(this.fields[key].options[i].value)
            }
          })
          formData[key] = values.length > 0 ? values.join(', ') : ''
          break
        }
        default: {
          formData[key] = this.fields[key].value
          break
        }
      }
    })
    return formData
  }

}

// Create a new instance of the ContactForm class
const form = new ContactForm(document.querySelector('.contact-form'))

// Initialise our instance of the ContactForm class when document has loaded
window.onload = (event) => form.init()

// form.init()

// class ContactForm {
  
//   constructor(config = {}) {
//     this.elements = {
//       form: config.form || null,
//       fields: config.form.elements || null
//     }
//     this.action = config.form.getAttribute('action') || null
//   }
  
//   init() {
//     this.elements.form.addEventListener('submit', (event) => {
//       return this.onSubmit(event);
//     });
//   }
  
//   onSubmit(event) {
//     event.preventDefault();
//     let data = this.getFormData();

//     this.sendFormData(data)
//       .then(response => {
//         if (response.status === 200) {
//           this.onSent('Message sent successfully!')
//         } else {
//           throw 'An error occured while sending the message.'
//         }
//       })
//     .catch(err => {
//       this.onError(err);
//     })
//   }
  
//   getFormData() {
//     let data = {},
//         id;

//     for (let i = 0; i < this.elements.fields.length; i++) {
//       id = this.elements.fields[i].id;
//       if (id !== '') data[id] = this.getFieldValue(this.elements.fields[i]);
//     };

//     return data;
//   }
  
//   getFieldValue(field) {
//     let values = [],
//         value;

//     switch(nodfield.typeeType) {
//       case 'radio':
//       case 'checkbox': {
//         value = field.checked;
//         break;
//       }
        
//       case 'select-one': {
//         if (!field[field.selectedIndex].disabled) {
//           value = field.value;
//         } else {
//           value = '';
//         }
//         break;
//       }
        
//       case 'select-multiple': {
//         for (let i = 0; i < field.options.length; i++) {
//           if (field.options[i].selected && !field.options[i].disabled) {
//             values.push(field.options[i].value)
//           }
//         }
//         value = values;
//         break;
//       }
        
//       default: {
//         value = field.value;
//         break;
//       }
//     }

//     return value;
//   }
  
//   sendFormData(data = {}) {
//     return new Promise((resolve, reject) => {
//       this.updateState('sending', 'Sending message...');
      // fetch(this.action, {
      //   headers: {
      //     'Content-Type': 'application/json',
      //   },
      //   method: 'POST',
      //   body: JSON.stringify(data)
      // })
      // .then(response => {
      //   resolve(response)
      // })
      // .catch(err => {
      //   reject(err)
      // })
//     })
//   }
  
//   onSent(message = '') {
//     let el = document.createElement('div');
//         el.classList.add('sent');
//         el.innerHTML = '<p>Message sent!</p>';
//     this.elements.form.appendChild(el);
//     this.updateState('sent');
//   }
  
//   onError(err = '') {
//     this.updateState('error', err);
//   }
  
//   updateState(state = 'default', message = '') {
//     this.elements.form.setAttribute('data-state', state);
//     this.elements.form.querySelector('small').textContent = message;
//   }

// }

// const form = new ContactForm({
//   form: document.querySelector('form')
// });

// form.init();
              
            
!
999px

Console