Skip to content

Instantly share code, notes, and snippets.

View gpassarelli's full-sized avatar

Gabriel Passarelli gpassarelli

View GitHub Profile
@gpassarelli
gpassarelli / gist:2134936
Created March 20, 2012 12:47
jQuery: Highlight All Links To Current Page
$(function(){
$("a").each(function(){
if ($(this).attr("href") == window.location.pathname){
$(this).addClass("selected");
}
});
});
@gpassarelli
gpassarelli / gist:2134944
Created March 20, 2012 12:48
jQuery: jQuery JSON getting with error catching
$.get('/path/to/url', function (data) {
if( !data || data === ""){
// error
return;
}
var json;
try {
json = jQuery.parseJSON(data);
} catch (e) {
// error
@gpassarelli
gpassarelli / gist:2134950
Created March 20, 2012 12:48
jQuery: Highlight Related Label when Input in Focus
$("form :input").focus(function() {
$("label[for='" + this.id + "']").addClass("labelfocus");
}).blur(function() {
$("label").removeClass("labelfocus");
});
@gpassarelli
gpassarelli / gist:2134959
Created March 20, 2012 12:49
jQuery: Persistant Headers on Tables
function UpdateTableHeaders() {
$("div.divTableWithFloatingHeader").each(function() {
offset = $(this).offset();
scrollTop = $(window).scrollTop();
if ((scrollTop > offset.top) && (scrollTop < offset.top + $(this).height())) {
$(".tableFloatingHeader", this).css("visibility", "visible");
$(".tableFloatingHeader", this).css("top", Math.min(scrollTop - offset.top, $(this).height() - $(".tableFloatingHeader", this).height()) + "px");
}
else {
$(".tableFloatingHeader", this).css("visibility", "hidden");
@gpassarelli
gpassarelli / gist:2134970
Created March 20, 2012 12:49
jQuery: Inputs That Remember Original Value
var origValue = [];
$('input.remember').each ( function (currentIndex)
{
origValue.push ( $(this).val () );
$(this).focus ( function ()
{
$(this).removeClass("unfocused");
var defaultText = $(this).val();
if ( $(this).val () == origValue [ currentIndex ] )
{
@gpassarelli
gpassarelli / gist:2134976
Created March 20, 2012 12:49
jQuery: Fire Event When User is Idle
idleTimer = null;
idleState = false;
idleWait = 2000;
(function ($) {
$(document).ready(function () {
$('*').bind('mousemove keydown scroll', function () {
@gpassarelli
gpassarelli / gist:2134986
Created March 20, 2012 12:50
jQuery: Zebra Stripe a Table
$("table#id tr:odd").addClass("odd");
@gpassarelli
gpassarelli / gist:2135071
Created March 20, 2012 12:56
jQuery: Check if Checkbox is Checked
/* Find out if a single checkbox is checked or not, returns true or false:*/
$('#checkBox').attr('checked');
/* Find all checked checkboxes: */
$('input[type=checkbox]:checked');
@gpassarelli
gpassarelli / gist:2135086
Created March 20, 2012 12:56
jQuery: Cycle Through a List
$(document).ready(function() {
var j = 0;
var delay = 2000; //millisecond delay between cycles
function cycleThru(){
var jmax = $("ul#cyclelist li").length -1;
$("ul#cyclelist li:eq(" + j + ")")
.animate({"opacity" : "1"} ,400)
.animate({"opacity" : "1"}, delay)
.animate({"opacity" : "0"}, 400, function(){
@gpassarelli
gpassarelli / gist:2135098
Created March 20, 2012 12:57
jQuery: Disable Parent Links in Nested List Navigation
$("#navigation li:has(ul.sub-navigation)").hover(function () {
$(this).children("a").click(function () {
return false;
});
});