Skip to content

Instantly share code, notes, and snippets.

View jlittlejohn's full-sized avatar

Josh Littlejohn jlittlejohn

View GitHub Profile
@jlittlejohn
jlittlejohn / gist:4280029
Created December 13, 2012 21:21
JS: Toggle Visibility
$("a.register").on("click", function(e){
$("#signup").fadeToggle(750, "linear");
});
@jlittlejohn
jlittlejohn / gist:4280051
Created December 13, 2012 21:24
JS: Loading External Content
$("#content").load("somefile.html", function(response, status, xhr) {
// error handling
if(status == "error") {
$("#content").html("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
@jlittlejohn
jlittlejohn / gist:4280086
Created December 13, 2012 21:26
JS: Keystroke Events
$('input').keydown(function(e) {
// variable e contains keystroke data
// only accessible with .keydown()
if(e.which == 11) {
e.preventDefault();
}
});
$('input').keyup(function(event) {
// run other event codes here
@jlittlejohn
jlittlejohn / gist:4280134
Created December 13, 2012 21:30
JS: Equal Column Heights
var maxheight = 0;
$("div.col").each(function(){
if($(this).height() > maxheight) { maxheight = $(this).height(); }
});
$("div.col").height(maxheight);
@jlittlejohn
jlittlejohn / gist:4280152
Created December 13, 2012 21:32
JS: Append New HTML
var sometext = "here is more HTML";
$("p#text1").append(sometext); // added after
$("p#text1").prepend(sometext); // added before
@jlittlejohn
jlittlejohn / gist:4280177
Created December 13, 2012 21:35
JS: Setting & Getting Attributes
var alink = $("a#user").attr("href"); // obtain href attribute value
$("a#user").attr("href", "http://www.google.com/"); // set the href attribute to a new value
$("a#user").attr({
alt: "Google",
href: "http://www.google.com/"
}); // set more than one attribute to new values
@jlittlejohn
jlittlejohn / gist:4280183
Created December 13, 2012 21:37
JS: Retrieve Content Values
$("p").click(function () {
var htmlstring = $(this).html(); // obtain html string from paragraph
$(this).text(htmlstring); // overwrite paragraph text with new string value
});
var value1 = $('input#username').val(); // textfield input value
var value2 = $('input:checkbox:checked').val(); // get the value from a checked checkbox
var value3 = $('input:radio[name=bar]:checked').val(); // get the value from a set of radio buttons
@jlittlejohn
jlittlejohn / gist:4280198
Created December 13, 2012 21:39
JS: Traversing the DOM
$("div#home").prev("div"); // find the div previous in relation to the current div
$("div#home").next("ul"); // find the next ul element after the current div
$("div#home").parent(); // returns the parent container element of the current div
$("div#home").children("p"); // returns only the paragraphs found inside the current div
@jlittlejohn
jlittlejohn / gist:4347564
Created December 20, 2012 18:36
SCSS: Disable Text Selection (Mixin)
@mixin disable-text-selection() {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
@jlittlejohn
jlittlejohn / gist:4381605
Created December 26, 2012 17:20
JS: Make Div Height Equal to Window
var height = $(window).height() - 25;
$('#sidebar').height(height - 170);
window.onresize = function(event) {
var height = $(window).height() - 25;
$('#sidebar').height(height - 170);
}