Skip to content

Instantly share code, notes, and snippets.

@afahy
afahy / decimalmark.js
Created February 14, 2011 17:23
Add a comma to a number
function decimalmark(num){
return num.toString().split("").reverse().join("").replace(/(\d{3})/g, "$1,").replace(/,$/, "").split("").reverse().join("");
}
@afahy
afahy / typeout.js
Created February 4, 2011 17:10
Video Game -style text type effect
// todo:
// // allow html
// msg = text string
// lps = letters per second
// see eg http://jsfiddle.net/afahy/NGPYz/2/
$.fn.typeout = function(msg, lps){
var textLen, timer, interval, that, currStep;
@afahy
afahy / jquery-tmpl-extension.js
Created October 20, 2010 16:30
Extends jquery-tmpl to add "set" and "exec" template tags
/*
Probably dangerous extensions to jquery-tmpl, allows you to set variables and exec code
* so not tested--don't blame me when it explodes
* see eg http://jsfiddle.net/afahy/GmGsp/2/
Usage: adds {{set}} and {{exec}} template tags to templates
{{set(varname) value}}
{{=varname}}
@afahy
afahy / app.js
Created September 7, 2010 20:30
Jumpstart scaffolding for initial dev on jQuery projects
/* jQuery dev scaffolding */
(function(global, $){
var doc = global.document;
$.noConflict();
$.bindReady = $.fn.ready;
$.ele = $.fn.init;
$.docRoot = new $.ele(doc);
@afahy
afahy / Numerics-only key event handler
Created July 9, 2010 20:17
Numerics-only key event handler
// to be used in eg:
// element.addEventListener("keypress", numericOnly, false);
var numericOnly = function(e){
var key = e.charCode || e.keyCode;
if((key < 37 || key > 40) && ((key > 19 && key < 48) || (key > 57 && key < 126))) {
e.preventDefault();
this.value = this.value.replace(/[^0-9]/g, "");
}
};
@afahy
afahy / delegate.js
Created June 28, 2010 14:15
Simple delegate extension for HTMLElements
// See eg http://jsfiddle.net/sCQxU/1/
HTMLElement.prototype.delegate = function(type, filter, callback){
var fn = function(e){
var ele = e.target;
if(filter(ele)){
callback.apply(ele, arguments);
}
}
this.addEventListener(type, fn, true);
@afahy
afahy / quick jquery func to toggle details structure
Created April 12, 2010 20:12
quick jquery func to toggle details structure
/*
Toggles visibility of content by clicking .summary
Given eg:
​<div class="details">
<p class="summary">Summary</p>
<p>Full</p>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
@afahy
afahy / log.js
Created April 2, 2010 19:31
Simple plugin to log / dir jquery objects
// allows you to log an object while staying in the chain
(function($){
var console = ("console" in window) ? window.console : { "log": alert, "dir": alert };
$.extend($.fn, {
log: function(){
console.log(this);
return this;
},
@afahy
afahy / gist:315955
Created February 26, 2010 17:53 — forked from paulirish/gist:315916
// everyone's new favorite closure pattern:
(function(window,document,undefined){ ... })(this,this.document);
// minified:
(function(b,a,c){ ... })(this,this.document);
// which means all uses of window/document/undefined inside the closure
// will be single-lettered, so big gains in minification.
// it also will speed up scope chain traversal a tiny tiny little bit.
@afahy
afahy / timer.js
Created February 25, 2010 18:06
Simple timer function
// simple timer function, piggybacking on jquery animate()
// accepts normal animate callbacks, like step and complete, props like duration
// returns object with props for percent complete and start time
(function($){
$.timer = (function(){
var f = function(params){