Last active
August 29, 2015 14:05
-
-
Save jimmylatreille/0565e917f6657d6d55f5 to your computer and use it in GitHub Desktop.
jquery learning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//namespacing event | |
$( 'li' ).on( 'click.logging', function() { | |
console.log( 'a list item was clicked' ); | |
}); | |
$( 'li' ).on( 'click.analytics', function() { | |
doSomethingElse(); | |
}); | |
$( 'li' ).off( 'click.logging' ); | |
//=========================================== | |
//event value | |
$( document ).on( 'click', function( event ) { | |
console.log( event.type ); // The event type, eg. "click" | |
console.log( event.which ); // The button or key that was pressed. | |
console.log( event.target ); // The originating element. | |
console.log( event.pageX ); // The document mouse X coordinate. | |
console.log( event.pageY ); // The document mouse Y coordinate. | |
}); | |
//=========================================== | |
//create and modify jQuery default option fx | |
// re-set an existing predefined speed | |
jQuery.fx.speeds.fast = 50; | |
// create a new pre-defined speed | |
jQuery.fx.speeds.turtle = 3000; | |
// Since we've re-set the 'fast' speed, this will now animate over the | |
// course of 50 milliseconds | |
$( '.hidden' ).hide( 'fast' ); | |
// After they are created, we can use custom speeds just as we use the | |
// built-in speeds | |
$( '.other-hidden' ).show( 'turtle' ); | |
//============================================= | |
//ajax | |
// Create the "callback" functions that will be invoked when... | |
// ... the AJAX request is successful | |
var updatePage = function( resp ) { | |
$( '#target').html( resp.people[0].name ); | |
}; | |
// ... the AJAX request fails | |
var printError = function( req, status, err ) { | |
console.log( 'something went wrong', status, err ); | |
}; | |
// Create an object to describe the AJAX request | |
var ajaxOptions = { | |
url: '/data/people.json', | |
dataType: 'json', | |
success: updatePage, | |
error: printError | |
}; | |
// Initiate the request! | |
$.ajax(ajaxOptions); | |
//or other method | |
$.get( '/data/people.html', function( html ){ | |
$( '#target' ).html( html ); | |
}); | |
$.post( '/data/save', { name: 'Rebecca' }, function( resp ) { | |
console.log( resp ); | |
}); | |
//other ajax trick | |
$( 'form' ).submit(function( event ) { | |
event.preventDefault(); | |
var form = $( this ); | |
$.ajax({ | |
type: 'POST', | |
url: '/data/save', | |
data: form.serialize(), | |
dataType: 'json', | |
success: function( resp ) { | |
console.log( resp ); | |
} | |
}); | |
}); | |
//========================================================= | |
//JSON PARSER | |
JSON.stringify(); | |
jQuery.parseJSON(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment