123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>Minimal Form Interface, what?</h1>
<p>
	jQuerified from <cite><a rel="source" href="https://tympanus.net/Development/MinimalForm/">Original Source</a></cite>
</p>
<hr />
 
<!-- form#demo>(div>(span>label[for=q$]{Question $})+input[id=q$][type=text])*6 -->
<form id="demo" class="minform">
	<section class="content"></section>
	<section class="controls">
		<button class="next">&rArr;</button>
		<div class="progress"></div>
		<span class="num">
			<span class="current"></span>
			<span class="total"></span>
		</span>
		<span class="error"></span>
	</section>
</form>

<script type="text/template" id="tpl-field">
	<div class="q step">
		<span>
			<label for="q{{i}}">{{text}}</label>
		</span>
		<input type="text" id="q{{i}}" name="q{{i}}" />
	</div>
</script>
<script type="text/template" id="tpl-answered">
	<dl class="a">
		<dt>{{q}}</dt>
		<dd>{{a}}</dd>
	</dl>
</script>

            
          
!
            
              * { box-sizing: border-box; }
// TODO: normalize

//#region ------ required ----------
.minform {
	.step, .error, .next {
		display:none;
	}
	.progress { height:3px; background-color:#000; width:0px; }
	.show { display:block; }
}
//#endregion ------ required ----------


//#region ------ nice ----------

$color-btn: #c8c8c8;
$color-error: #f00;
$color-prog: green;

#demo { font-size:24px; }
.minform { position:relative; }
.q label, .q input { display:block; }
.q input { width:100%; height:2em; font-size:150%; padding:0.25em; border:none; background-color:$color-btn; }
button.next { position:absolute; 
			  // adjust size and position so it fits within the input box
			  top:1.5em; 
			  right: 3px; 
			  height:2em; 
			  font-size:100%; border:none; background-color:$color-btn; }
.num {
	position:absolute; right:0; bottom: -1em; color:$color-prog; 
	.total:before { content: '/ '; }
}
.minform .progress { background-color:$color-prog; }
.error { color:$color-error; position:absolute; bottom:-1em; }


// fake submit
.minform.complete {
	section.content, button.next { display:none; }
}
pre { white-space:pre-wrap; }


//#endregion ------ nice ----------
            
          
!
            
              /* ---------- THE PLUGIN ---------- */
/**
 * stepsForm.jquery.js v1.1
 * 
 * from stepForm.js v1.0.0
 * http://www.codrops.com via https://tympanus.net/Development/MinimalForm/
 *
 * See original source for license, etc
 */
; (function (window, $, Modernizr) {

	'use strict';

	var transEndEventNames = {
		'WebkitTransition': 'webkitTransitionEnd',
		'MozTransition': 'transitionend',
		'OTransition': 'oTransitionEnd',
		'msTransition': 'MSTransitionEnd',
		'transition': 'transitionend'
	},
		transEndEventName = transEndEventNames[Modernizr.prefixed('transition')],
		support = { transitions: Modernizr.csstransitions },
		proto = {},
		defaults = {
			// class name {
			active: 'show',
			ready: 'show-next',
			// selectors { =====
			question: '.step',
			next: 'button.next',
			// progress indicator
			progress: '.progress',
			// step counter + placeholders
			counter: 'span.num',
				index: '.current',
				total: '.total',
			// error message container
			error: 'span.error',
			// how to find the field inputs
			inputs: 'input, select, textarea',
			// callbacks { =====
			invalid: function (answer, $input) {
				// check the input value and return the failure message if invalid
        
           return answer === '' ? 'EMPTYSTR' : false;
			},
			onSubmit: $.noop
		};

	function stepsForm(el, options) {
		this.el = el;
		this.options = $.extend({}, defaults, options);
		this._init();
	}

	proto._init = function () {
		var o = this.options, el = this.el; // shorthand
		// current question
		this.current = 0;

		// questions
		this.questions = el.find(o.question);
		// total questions
		this.questionsCount = this.questions.length;
		// show first question
		this.questions.first().addClass(o.active);

		// next question control
		this.ctrlNext = el.find(o.next);

		// progress bar
		this.progress = el.find(o.progress);

		// index container -- so we can transition the current counter
		this.questionStatus = el.find(o.counter);
		// current question placeholder
		this.currentNum = this.questionStatus.find(o.index);
		this.currentNum.html(this.current + 1);
		// total questions placeholder
		this.totalQuestionNum = this.questionStatus.find(o.total);
		this.totalQuestionNum.html(this.questionsCount);

		// error message
		this.error = el.find(o.error);

		// init events
		this._initEvents();
	};

	proto._initEvents = function () {
		var self = this,
			// first input
			firstElInput = this.questions.eq(this.current).find(self.options.inputs);

		// show the next question control first time the input gets focused
		firstElInput.one('focus', function () {
			self.ctrlNext.addClass(self.options.active);
		});

		// show next question
		self.ctrlNext.on('click', function (ev) {
			ev.preventDefault();
			self._nextQuestion();
		});

		// hotkeys
		self.el.on('keydown', function (ev) {
			switch(ev.keyCode || ev.which) {
				// pressing enter will jump to next question
				case 13:
					ev.preventDefault();
					self._nextQuestion();
					break;
				// disable tab
				case 9:
					ev.preventDefault();
					break;
			}
		});
	};

	proto._nextQuestion = function () {
		if (this.isFilled === true || !this._validade()) {
			return false;
		}

		var self = this;

		// check if form is filled
		if (self.current === self.questionsCount - 1) {
			self.isFilled = true;
		}

		// clear any previous error messages
		self._clearError();

		// update progress bar
		self._progress();

		// skip some ui changes if we're done
		if (!self.isFilled) {
			// question references (note incremented question index
			var currentQuestion = self.questions.eq(self.current),
				nextQuestion = self.questions.eq(++self.current);

			// change the current question number/status
			self._updateQuestionNumber();

			// add class "show-next" to form element (start animations)
			self.el.addClass(self.options.ready);

			// remove class "current" from current question and add it to the next one
			currentQuestion.removeClass(self.options.active);
			nextQuestion.addClass(self.options.active);
		}

		// after animation ends, remove class "show-next" from form element and change current question placeholder
		var onEndTransitionFn = function (ev) {
				if (support.transitions) {
					$(this).off(transEndEventName, onEndTransitionFn);
				}
				if (self.isFilled) {
					self._submit();
				}
				else {
					self.el.removeClass(self.options.ready);
					self.currentNum.html( self.nextQuestionNum.html() );
					self.nextQuestionNum.remove();
					// force the focus on the next input
					nextQuestion.find(self.options.inputs).focus();
				}
			};

		if (support.transitions) {
			self.progress.on(transEndEventName, onEndTransitionFn);
		}
		else {
			onEndTransitionFn();
		}
	}

	// updates the progress bar by setting its width
	proto._progress = function () {
		this.progress.css('width', (this.current+1) * (100 / this.questionsCount) + '%');
	}

	// changes the current question number
	proto._updateQuestionNumber = function () {
		// first, create next question number placeholder
		this.nextQuestionNum = $('<span />', { className: this.options.nextnum }).text(this.current + 1);
		// insert it in the DOM
		this.questionStatus.append(this.nextQuestionNum);
	}

	// submits the form
	proto._submit = function () {
		this.options.onSubmit.call(this);
	}

	// TODO (next version..)
	// the validation function
	proto._validade = function () {
		// current question´s input
		var input = this.questions.eq(this.current).find(this.options.inputs);
		var invalid = this.options.invalid(input.val(), input);
		if (invalid) {
			this._showError(invalid);
			return false;
		}

		return true;
	}

	// TODO (next version..)
	proto._showError = function (message) {
		// some convenience placeholders
		switch (message) {
			case 'EMPTYSTR':
				message = 'Please fill the field before continuing';
				break;
			case 'INVALIDEMAIL':
				message = 'Please fill a valid email address';
				break;
		};
		this.error.addClass(this.options.active).html(message);
	}

	// clears/hides the current error message
	proto._clearError = function () {
		this.error.removeClass(this.options.active);
	}

	// bind prototype
	$.extend(stepsForm.prototype, proto);

	// expose
	$.fn.stepsForm = function(options) {
		this.each(function() {
			new stepsForm($(this), options);
		});
	}
  $.stepsFormDefaults = defaults; // so you can reuse default validation fns, etc

})(window, jQuery, typeof Modernizr === 'undefined' ? { prefixed: $.noop } : Modernizr);

/* ---------- THE DEMO ------------ */
// need to wrap it to prevent codepen conflict
(function($) {

var $container = $('#demo > .content'), field_template = $('#tpl-field').html();

// quick lodash
var each = function(args, cb) {
	for (var k in args) if (args.hasOwnProperty(k)) if( false === cb(args[k], k) ) return;
}
// quick Moustache
var Template = function (template, args) {
	each(args, function(replacement, placeholder) {
		template = template.replace(new RegExp('{{' + placeholder + '}}', 'gm'), replacement);
	});

	return template;
}

var questions = [
	'What is your favorite color?',
	'Who\'s your daddy?',
   'What do mice eat?',
	'Square root of PI?',
	'What\'s the <code>symlink</code> to your heart?'
];

each(questions, function(q, i) {
	$container.append(Template(field_template, { i: i, text: q }));
});

$('#demo').stepsForm({
   invalid: function(answer, $input) {
		// joke example
     if(answer.indexOf('cheese') >= 0) return 'You can\'t say that...';
           
     // reuse base
	  return $.stepsFormDefaults.invalid(answer, $input);
   },
	onSubmit: function() {
		var data = this.el.serializeArray(), display = [];
		this.el.addClass('complete');
		if (window.console && console.log) console.log('Submitting Minimal Form Demo:', data);
		this.el.before('<strong>Thanks for playing the game!  Refresh to try again...</strong>');

		var ans_template = $('#tpl-answered').html();

		each(data, function (v) {
			display += Template(ans_template, { q: questions[parseInt(v.name.substring(1))], a: v.value });
		});
		this.el.after('<div style="margin-top:3em;"><em>You Said:</em>' + display + '</div>');
	}
});
  
})(jQuery);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console