static void

jQuery

OnLoad

$(document).ready(function () {
	//document is ready
}
//...but this is the same and shorter...
$(function(){
   //document is ready
 });
//(actually it's a callback from the ready event)

Events

Creating Html

Alternatives to innerHTML

$('div#target').html('<p class="New">New html!</p>');
$('div#target').add('<p class="New">New html!</p>'); //the same
//You can also create a jQuery object from an html string
$('<p class="New">New html!</p>').appendTo($('div#target'));

$('div#target').wrap("<div class='marker'/>") is also useful.

Nested searches

$('div#target').children('p').addClass("x"); //immediate descendents
$('div#target').find('p').addClass("x"); //any descendents
$('div#target').click(function() { 
	$('p', this).addClass("x"); //selector context
	$(this).find('p').addClass("x"); //is the same
});

If you're doing lots of chaining, .end() is nice for popping up the chain.

Selectors

Some unusual variations.

Check selector found something:

if($("#field).length) {}

Iteration

$('div').each(function(index) {
	//iterate over a jQuery object
});
//NOT THE SAME AS...
$.each(myArray, function(index, value) { 
  //iterate over an array (or map, in which case index==key)
});

Array functions

Show/Hide

Forms and Ajax

Serializing forms

For ajax / HTTP gets

Ajax post

var data = $("form").serializeArray(); //creates an array of all form elements
var json = JSON.stringify(d); //if IE7 or older you'll need json2.js; native in modern browsers
$.ajax({
    type: "POST",
    contentType: "application/json",
    url: "WebService.asmx/WebMethodName",
    data: json,
    success: function (x) { alert(x.Message); }
});

Ajax getting data

$.getJSON('WebService.asmx/WebMethodName', function(data) { ..}) doesn't allow any posts (input data) and parses the output with $.parseJSON().

On the lower level $.ajax() call, you can JSON.parse(s).

You can register a page handler for .ajaxError