Skip to content

Instantly share code, notes, and snippets.

View jimfleming's full-sized avatar

Jim Fleming jimfleming

View GitHub Profile
@jimfleming
jimfleming / Ellipsis.js
Created May 26, 2010 19:34
Automatically truncates a string and appends an ellipsis to the end.
function ellipsis(string, length) {
return string.length > length ? (string.substr(0, length - 3) + '...') : string;
}
@jimfleming
jimfleming / CalculateSavings.js
Created May 26, 2010 19:35
Calculates the difference between two dollar values with close approximation.
function calculate_savings(cost, sale) {
cost *= 100;
sale *= 100;
return ((cost - sale) / 100).toFixed(2);
}
@jimfleming
jimfleming / Auto-select Fields
Created July 20, 2010 17:02
Auto-select next field as the user (de-)fills them. Useful for three phone number fields.
var $phone1 = $('input#phone1');
var $phone2 = $('input#phone2');
var $phone3 = $('input#phone3');
$phone1.change(function() {
if ($phone1.val().length == 3) // if they've typed 3 characters
$phone2.focus(); // select the next field
});
$phone2.change(function() {
@jimfleming
jimfleming / Default Text jQuery Plugin
Created July 20, 2010 17:07
Default Text jQuery Plugin
(function($) {
$.fn.defaultText = function(settings) {
var defaults = { 'color' : '#ccc',
'default_text' : 'Default Text'
};
if (settings) $.extend(defaults, settings);
this.each(function() {
$this = $(this);
@jimfleming
jimfleming / console.log override for IE
Created July 23, 2010 14:10
Since IE is dumb and doesn't support console.log this will check if console.log already exists, if not it will create a function that alerts
if (typeof console == 'undefined' && typeof console.log == 'undefined') console = { log : function(message) { alert(message) } }
@jimfleming
jimfleming / fix_has_layout.js
Created July 26, 2010 17:36
Applies "zoom: 1" to block elements
var fix_has_layout = function(container) {
var elements = $('*', container || document);
elements.each(function(element) {
if (element.css('display') == 'block')
element.css('zoom', 1);
});
}
$(function() {
@jimfleming
jimfleming / Slideshow jQuery Plugin.js
Created August 10, 2010 21:44
A simple slideshow plugin for jQuery
(function($) { // this ensures that the plugin doesn't conflict with other code and is recommended for jQuery plugins: http://docs.jquery.com/Plugins/Authoring
$.fn.slideshow = function(settings) { // declare the plugin function accepting a single argument 'settings' to override any of the defaults
var config = { 'delay' : 4000, 'active_class' : 'active', 'navigation_selector' : '#navigation a' }; // some defaults which can be overridden by 'settings' above
if (settings) $.extend(config, settings); // merge the defaults with the settings passed in above
this.each(function() { // for each jQuery object matching the selector (e.g. $('ul#some-list > li').each(function() {}); would call the function inside passing in each element as 'this')
var $this = $(this); // to prevent recreating a jQuery object every time I need $(this)
$this.css('position', 'relative'); // set the slide wrapper as relative
@jimfleming
jimfleming / Scroller jQuery Plugin.js
Created August 11, 2010 15:49
Similar to a slideshow but scrolls horizontally with left and right navigation
(function($) {
$.fn.scroller = function(settings) {
var config = { 'delay' : 4000, 'prev_selector' : '#prev', 'next_selector' : '#next', 'should_loop' : false };
if (settings) $.extend(config, settings);
this.each(function() {
var $this = $(this);
var current = 0;
var head = document.getElementsByTagName('head')[0];
var meta = document.createElement('meta');
meta.setAttribute('http-equiv', 'X-UA-Compatible');
meta.setAttribute('content', 'IE=EmulateIE7');
head.appendChild(meta);
var json_escape = function(value) {
if (value.replace)
return value.replace(/\//g, '\\/')
else
return value
}
var json_to_html = function(json, level) {
level = level || 0