Skip to content

Instantly share code, notes, and snippets.

View elijahmanor's full-sized avatar
😀

Elijah Manor elijahmanor

😀
View GitHub Profile
// Sold on jQuery - just re-wrote the following Prototype code:
Element.observe(window, 'load', function(){
// This is for slow forms (like the signup one),
// so the user doesn't click 'Submit' twice.
$$('form.slow').each(function(frm){
var sub = frm.down("input[type='submit']");
if(sub) sub.disabled = '';
$(frm).observe('submit', function(e){
var sub = frm.down("input[type='submit']");
(function($){
$.PicasaWebViewer = {
jQuery : $,
defaultOptions : {
albumClass: "albumClass",
photoClass: "photoClass",
urlFormat: "http://picasaweb.google.com/data/feed/api/user/{0}?alt=json-in-script",
userName: "elijah.manor"
},
//Quick Element Contruction Syntax
//Instead of concatenating strings together to build up new HTML elements,
//you can now utilize a new feature inside of jQuery 1.4 for quick element construction.
//You can pass a map of properties as the second parameter to set attributes and wire-up event handlers
//Keep in mind certain elements cannot be created this way due to limitations in some browsers (Internet
//Explorer for example). Elements you will have trouble with include `input` and `button` elements.
$("<a />", {
id: "companyLink",
//Using Namespaced Events
//When writing jQuery Plugins it is helpful to add a namespace when binding to native or custom events.
//You can unbind all events associated with your plugin without affecting other events.
//The namespace will not affect triggering custom events. They will work as you would expect.
//Use a namespace with your plugin events
$("div").bind("click.myPlugin", func1);
$("div").bind("keydown.myPlugin", func2);
$("div").unbind(".myPlugin");
//Changing Event Handler Context
//You can access the context by using the "this" pseudo parameter. The context of most jQuery Event Handler's
//is the DOM element that caused the event. There may be times when you want to change the context (the value
//of "this") inside of your event handler. In that case, you should use the $.proxy() method. The $.proxy()
//method allows you to manually define what you want the context to be inside your event handler.
//Utilizing the $.proxy() method to change context
var contact = {
name: "jQuery",
@elijahmanor
elijahmanor / gist:589968
Created September 21, 2010 16:26
Use === and !==
// Use === and !==
// It is often better to use the === and !== operators
// rather than == and != operators. The reason for this
// is that === and !== (also known as the identity operators)
// check for the type as well as the value when being compared
// whereas the == and != will try to coerce the two values
// into the same type before the comparison is made, which
// may lead to some very unexpected results.
@elijahmanor
elijahmanor / gist:590010
Created September 21, 2010 16:44
Check hasOwnProperty in for...in
// Check hasOwnProperty in for...in
// When you are using the for...in loop in JavaScript
// you must be careful to also check the object
// you are enumerating to see if it contains
// the property before proceeding. The reason for
// this is that the for...in loop also iterates
// over the prototype's properties as well */
var Person = function( firstName, lastName ) {
@elijahmanor
elijahmanor / gist:590041
Created September 21, 2010 17:01
Prevent Default Behavior
// Prevent Default Behavior
// 'return false' stops the default behavior of the event
// and prevents it from bubbling up DOM, which kills
// event delegation and may not be what you indented.
// You might consider using e.preventDefault() or
// e.stopPropagation() instead
//event.preventDefault()
//Event Delegation Friendly
@elijahmanor
elijahmanor / toggle_jsbin_and_gist.js
Created July 2, 2011 19:03
Toggle JS Bin & Gist
//http://userjs.up.seesaa.net/js/bookmarklet.html
(function() {
var location = document.location.href,
newLocation = null;
if ( ~location.indexOf( "gist.github.com" ) ) {
console.log( "Switching from gist to jsbin..." );
newLocation = location.replace( /https:\/\/gist.github.com\/(\d+)/, function( str, group1 ) {
@elijahmanor
elijahmanor / jquery.selectbydata.js
Created July 19, 2011 04:46
Selector Using HTML5 Data Attributes
var valueImLookingFor = "myvalue";
//The string concatenation just feels wrong to me...
$( "p[data-mytype" + "='" + valueImLookingFor + "']" )
.doSomething();