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

              
                <h3 id="status"></h3>
<textarea id="comment"></textarea>
              
            
!

CSS

              
                #comment {
  height: 100px;
  width: 500px;
}

body {
  background-color: #eee;
}
              
            
!

JS

              
                

$(document).ready(function () {
  
  // Invoke our plugin and get reference to the returned API object.
  var autosaveApi = $('#comment').autosave({
    // Pass in our own custom save URL.
    saveUrl: 'https://codepen.io',
    // Pass in a callback to be invoked whenever the status changes.
    statusChanged: function (status) {
      $('#status').text(status.text).css('color', status.color);
    }
  });
  
  // Use the autosave API to query for the initial saved status and set our label.
  var initialStatus = autosaveApi.getStatus();
  $('#status').text(initialStatus.text).css('color', initialStatus.color);
});


// -=-=-= AUTOSAVE PLUGIN =-=-=-

// Create a self-invoking function to preserve the jQuery dollar sign.
(function ($) {
  // Create the autosave plugin.
  $.fn.autosave = function (userOptions) {
    var $textarea = this;
    window.$textarea = this;
    
    // Setup our plugin's default options.
    var defaultOptions = {
      saveUrl: null,
      status: {
        text: 'no changes',
        color: '#009'
      },
      statusChanged: null
    };
    
    // Override default options with any provided by user.
    var options = $.extend({}, defaultOptions, userOptions);
    
    // Create a function to save the user's content.
    var save = function () {
      
      // Make a call to our server endpoint to save the user's text.
      $.ajax({
        url: options.saveUrl,
        type: 'POST',
        data: { content: $textarea.val() },
        
        // If successful then change the save status and invoke the user's callback.
        success: function () {
          options.status = {
            text: 'saved',
            color: '#090'
          };
          if (options.statusChanged) {
            options.statusChanged(options.status);
          }
        },
        
        // If an error occurred then change the save status and invoke the user's callback.
        error: function () {
          options.status = {
            text: 'error saving',
            color: '#f00'
          }
          if (options.statusChanged) {
            options.statusChanged(options.status);
          }
        }
      })
    };
    
    
    // Create a variable to track our timeout ID.
    var timeoutId;
    
    // Setup an event handler to listen for keyup events in the textarea.
    $textarea.keyup(function (e) {
      
      // If we have a timeoutId then use it to clear the existing timeout.
      if (timeoutId) {
        clearTimeout(timeoutId);
      }
      
      // Set our saved status.
      options.status = {
        text: 'pending save...',
        color: '#770'
      };
      if (options.statusChanged) {
        options.statusChanged(options.status);
      }
      
      // Start a new timeout that will save the user's content in 3/4 of a second.
      // If they type again this function will run, clear the timeout, and start a new one.
      // If they stop typing for 750 milliseconds then the timeout will finally fire and save.
      timeoutId = setTimeout(save, 750);
    });
    
    return {
      getStatus: function () {
        return options.status;
      }
    };
  };
})(jQuery);
              
            
!
999px

Console