Skip to content

Instantly share code, notes, and snippets.

@chrisdlangton
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdlangton/f1d9369e517583a06da0 to your computer and use it in GitHub Desktop.
Save chrisdlangton/f1d9369e517583a06da0 to your computer and use it in GitHub Desktop.
persist forms demo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="google" content="notranslate">
<meta name="robots" content="noindex,nofollow">
<title>github.com/chrisdlangton/persistJS</title>
<meta name="language" content="EN">
<script src="//code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<style type="text/css">
label { display: block; }
textarea { height: 80px; }
select[multiple] { height: 80px; }
select:not([multiple]), input[type=text], input[type=url], input[type=email], input[type=password], input[type=tel], input[type=number], input[type=time], input[type=date] {
height: 40px;
}
select, textarea, input[type=text], input[type=url], input[type=email], input[type=password], input[type=tel], input[type=number], input[type=time], input[type=date] {
-webkit-appearance: none;
-moz-appearance: none;
display: block;
margin: 0;
width: 100%;
line-height: 40px;
font-size: 17px;
border: 1px solid #bbb;
}
input[type=checkbox], input[type=radio] {
width: 44px;
height: 44px;
-webkit-border-radius: 22px;
-moz-border-radius: 22px;
border-radius: 22px;
border: 1px solid #bbb;
}
button[type=submit], button[type=button] {
-webkit-appearance: none;
-moz-appearance: none;
display: block;
margin: 1.5em 0;
font-size: 1em;
line-height: 2.5em;
color: #333;
font-weight: bold;
height: 2.5em;
width: 100%;
background: #fdfdfd;
background: -moz-linear-gradient(top, #fdfdfd 0%, #bebebe 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#fdfdfd), color-stop(100%,#bebebe));
background: -webkit-linear-gradient(top, #fdfdfd 0%,#bebebe 100%);
background: -o-linear-gradient(top, #fdfdfd 0%,#bebebe 100%);
background: -ms-linear-gradient(top, #fdfdfd 0%,#bebebe 100%);
background: linear-gradient(to bottom, #fdfdfd 0%,#bebebe 100%);
border: 1px solid #bbb;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
input[type=range] {
width: 100%;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 44px;
height: 44px;
background: #fdfdfd;
background: -moz-linear-gradient(top, #fdfdfd 0%, #bebebe 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#fdfdfd), color-stop(100%,#bebebe));
background: -webkit-linear-gradient(top, #fdfdfd 0%,#bebebe 100%);
background: -o-linear-gradient(top, #fdfdfd 0%,#bebebe 100%);
background: -ms-linear-gradient(top, #fdfdfd 0%,#bebebe 100%);
background: linear-gradient(to bottom, #fdfdfd 0%,#bebebe 100%);
border: 1px solid #bbb;
-webkit-border-radius: 22px;
-moz-border-radius: 22px;
border-radius: 22px;
}
/* unstyled tel links as default */
a[href^='tel:']:link, a[href^='tel:']:visited {
color: #6f757c;
font-weight: normal;
text-decoration: none;
}
a[href^='tel:']:hover, a[href^='tel:']:active {
color: #6f757c;
text-decoration: none;
}
/* styled tel links for small viewports */
@media screen and (max-width: 600px) {
a[href^='tel:']:link, a[href^='tel:']:visited {
color: #333;
font-weight: bold;
text-decoration: underline;
}
a[href^='tel:']:hover, a[href^='tel:']:active {
color: #333;
text-decoration: none;
}
}
</style>
</head>
<body>
<h1>persist form Demo</h1>
<form id="persistJS"
persist
method="GET"
action="./"
accept-charset="utf-8">
<label for="text">Text:</label>
<input id="text" type="text" name="text">
<label for="textList">Text List:</label>
<input id="textList" name="textList" type="text" list="numbers" autocorrect="off" autocapitalize="off">
<label for="range">Range:</label>
<input id="range" type="range" name="range" min="0" max="100" list="numbers">
<label for="number">Number:</label>
<input id="number" type="number" name="number" min="0" max="100" list="numbers">
<datalist id="numbers">
<option label="None">0</option>
<option label="25">25</option>
<option label="midpoint">50</option>
<option label="75">75</option>
<option label="All">100</option>
</datalist>
<label for="select">Select:</label>
<select id="select" name="select">
<option label="None">0</option>
<option label="Quarter">25</option>
<optgroup label="Mid">
<option label="midpoint" value="50">50</option>
<option value=".5">.5</option>
</optgroup>
<option label="Three Quarters" value="75">75</option>
<option label="All">100</option>
</select>
<label for="selectMulti">Select Multi:</label>
<select id="selectMulti" name="selectMulti[]" multiple="multiple">
<option label="None" value="0">0</option>
<option label="Quarter" value="25">25</option>
<optgroup label="Mid">
<option label="midpoint" value="50">50</option>
<option value=".5">.5</option>
</optgroup>
<option label="Three Quarters" value="75">75</option>
<option label="All" value="100">100</option>
</select>
<label for="website">Website:</label>
<input type="url" id="website" name="website" placeholder="www.yourwebsite.com" autocorrect="off" autocapitalize="off">
<label for="tel">Tel:</label>
<input id="tel" type="tel" name="tel" placeholder="Your telephone number">
<label for="email">Email:</label>
<input id="email" type="email" name="email" placeholder="you@yourwebsite.com" autocorrect="off" autocapitalize="off">
<label for="time">Time:</label>
<input id="time" type="time" name="time" placeholder="9:00 AM">
<label for="date">Date:</label>
<input id="date" type="date" name="date" placeholder="DD/MM/YYYY">
<label for="firstName">First:</label>
<input type="text" id="firstName" name="firstName" autocapitalize="words">
<label for="lastName">Last:</label>
<input type="text" id="lastName" name="lastName" autocapitalize="words">
<label for="password">Password:</label>
<input type="password" id="password" name="password" autocorrect="off" autocapitalize="off">
<label for="state">State:</label>
<input type="text" id="state" name="state" autocapitalize="characters">
<label>Radio:</label>
<input type="radio" name="radio" value="1">
<input type="radio" name="radio" value="2">
<label>Checkbox:</label>
<input type="checkbox" name="checkbox[]" value="1">
<input type="checkbox" name="checkbox[]" value="2">
<label for="comment">Comment:</label>
<textarea id="comment" name="comment" autocapitalize="sentences"></textarea>
<button type="button">Submit</button>
</form>
<script type="text/javascript" src="persistForms.min.js"></script>
</body>
</html>
!function(e,t,i){var n=function r(n){return e===this?new r(n):(this.options=t.extend({},this.options,n),t.fn.serializeObject=function(){var e=this,n={},r={},a={validate:/^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,key:/[a-zA-Z0-9_]+|(?=\[\])/g,push:/^$/,fixed:/^\d+$/,named:/^[a-zA-Z0-9_]+$/};return this.build=function(e,t,i){return e[t]=i,e},this.push_counter=function(e){return r[e]===i&&(r[e]=0),r[e]++},t.each(t(this).serializeArray(),function(){if(a.validate.test(this.name)){for(var r,o=this.name.match(a.key),s=this.value,c=this.name;(r=o.pop())!==i;)c=c.replace(new RegExp("\\["+r+"\\]$"),""),r.match(a.push)?s=e.build([],e.push_counter(c),s):r.match(a.fixed)?s=e.build([],r,s):r.match(a.named)&&(s=e.build({},r,s));n=t.extend(!0,n,s)}}),n},this)};n.fn=n.prototype={init:function(){var e=this;return t(document).on("input change","form[id] input,form[id] select,form[id] textarea",function(){var e=t(this).parent("form");PersistFormsInstance.db(e.prop("id"),e.serializeObject())}),t("form[id]").each(function(){var i=t(this),n=e.db(i.prop("id"));t("form[id] input,form[id] select,form[id] textarea").each(function(){var e=t(this),i=e.prop("name").replace(/\[\]+$/,"");if(null!==n&&n[i])if("SELECT"===this.tagName&&this.multiple)for(var r=0;r<n[i].length;r++)e.children("option").filter('[value="'+n[i][r]+'"]').prop("selected","selected");else if("radio"===this.type||"checkbox"===this.type)for(var a=0;a<n[i].length;a++)e.filter('[value="'+n[i][a]+'"]').prop("checked","checked");else e.val(n[i])})}),this},isJSON:function(e){return"string"!=typeof e||""===e?!1:/^[\],:{}\s]*$/.test(e.replace(/\\["\\\/bfnrtu]/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))},createCookie:function(e,t,i,n,r){"undefined"==typeof i&&(i=365),"string"!=typeof t&&(t=JSON.stringify(t));var a=e+"="+escape(t)+";";return i&&(i instanceof Date?isNaN(i.getTime())&&(i=new Date):i=new Date((new Date).getTime()+1e3*parseInt(i)*60*60*24),a+="expires="+i.toGMTString()+";"),n&&(a+="path="+n+";"),r&&(a+="domain="+r+";"),document.cookie=a,a},getCookie:function(e){var t=new RegExp("(?:^"+e+"|;s*"+e+")=(.*?)(?:;|$)","g"),i=t.exec(document.cookie);return null===i?null:this.isJSON(unescape(i[1]))?JSON.parse(unescape(i[1])):unescape(i[1])},db:function(t,i){var n;return function(){var t="testHasLocalStorage";try{return e.localStorage.setItem(t,t),e.localStorage.removeItem(t),!0}catch(i){return!1}}()===!0?"undefined"==typeof i?(n=e.localStorage.getItem(t),this.isJSON(n)?JSON.parse(n):n):e.localStorage.setItem(t,"string"!=typeof i?JSON.stringify(i):i):"undefined"==typeof i?(n=this.getCookie(t),this.isJSON(n)?JSON.parse(n):n):this.createCookie(t,"string"!=typeof i?JSON.stringify(i):i)}},e.PersistForms=n,PersistFormsInstance=(new n).init()}(window,jQuery);
//# sourceMappingURL=persistForms.min.js.map
{"version":3,"sources":["persistForms.js"],"names":["window","$","undefined","PersistForms","options","this","extend","fn","serializeObject","self","json","push_counters","patterns","validate","key","push","fixed","named","build","base","value","push_counter","each","serializeArray","test","name","k","keys","match","merge","reverse_key","pop","replace","RegExp","prototype","init","$that","document","on","$form","parent","PersistFormsInstance","db","prop","formData","$field","fieldName","tagName","multiple","i","length","children","filter","type","val","isJSON","createCookie","expires","path","domain","JSON","stringify","cookie","escape","Date","isNaN","getTime","parseInt","toGMTString","getCookie","regexp","result","exec","unescape","parse","item","text","mod","localStorage","setItem","removeItem","e","getItem","jQuery"],"mappings":"CAMA,SAAWA,EAAQC,EAAGC,GACpB,GAAIC,GAAe,QAASA,GAAaC,GACvC,MAAIJ,KAAWK,KACN,GAAIF,GAAaC,IAE1BC,KAAKD,QAAUH,EAAEK,UAAWD,KAAKD,QAAQA,GACzCH,EAAEM,GAAGC,gBAAkB,WACrB,GAAIC,GAAOJ,KACPK,KACAC,KACAC,GACEC,SAAY,wDACZC,IAAY,0BACZC,KAAY,KACZC,MAAY,QACZC,MAAY,kBA6BlB,OA3BAZ,MAAKa,MAAQ,SAAUC,EAAML,EAAKM,GAEhC,MADAD,GAAKL,GAAOM,EACLD,GAETd,KAAKgB,aAAe,SAAUP,GAG5B,MAFIH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