Skip to content

Instantly share code, notes, and snippets.

// simple equal column heights
// sets the height of each matched div to that of the tallest element in the collection
// Usage: $("#div1, #div2, #div3").equalHeights();
$.fn.equalHeights = function(){
var heights = [], i = this.length;
while(i--){ heights[i] = this.eq(i).height(); }
return this.height(Math.max.apply(Math, heights));
}
// determine if an element is COMPLETELY inside the current viewport.
// surely, there must be an easier way
$.expr[':']['in-viewport'] = function(element){
var $win = $(window), $el = $(element),
// element calculations
offset = $el.offset(),
outerWidth = $el.outerWidth(),
outerHeight = $el.outerHeight(),
// extremely basic templating with replace().
var
// template
template = '<div id="#{id}">#{text}</div>',
// object literal of replacement values
params = { id:10, text:'hi there' },
// Skeleton jQuery Plugin (OO)
(function($){
$.fn.myPlugin = function( options ){
options = $.extend( {}, $.fn.myPlugin.defaults, options );
return this.each(function(){
// create a new object & store it in the element's data for easy access
$(this).data('myPlugin', new MyPlugin(this, options));
// accessing all OTHER instances of a widget, from within your widget.
// method one: use your own data store
var instances = [];
$.widget("ui.dialog", {
_create: function(){
instances.push(this.element);
},
(function($){
$.widget("ui.mywidget", {
options: {
autoOpen: true
},
_create: function(){
// by default, consider this thing closed.
// recursive setTimeout, better than setInterval. @paul_irish
(function f(){ if(condition){} else{ setTimeout(f, 1000); } })().
// A
$this.find("li")
.wrapInner('<a href="#" style="display:block" />')
.delegate("a", "click", function(){
$this.dialog("close");
return false;
});
// B
// old and busted
var foo = $(".bar");
if( foo.length ){
foo.addClass("LOL");
}
// new hotness
// jquery related selects plugin by eric hynds, erichynds.com
// re-write of http://github.com/ehynds/jquery-related-selects
(function($){
$.fn.relatedSelects = function( options ){
function RelatedSelect( form, options ){
var selects = this.selects = [], form = $(form);