jQuery website jQuery documentation

JavaScript

$.extend(Object target, Object prop1, Object propN) : Object

targetObjectThe object to extend
prop1ObjectThe object that will be merged into the first.
propNObject(optional) More objects to merge into the first

Extend one object with one or more others, returning the original, modified, object. This is a great utility for simple inheritance.

Examples

Merge settings and options, modifying settings

Code
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
jQuery.extend(settings, options);
Result
settings == { validate: true, limit: 5, name: "bar" }

Merge defaults and options, without modifying the defaults

Code
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
var settings = jQuery.extend({}, defaults, options);
Result
settings == { validate: true, limit: 5, name: "bar" }

$.each(Object obj, Function fn) : Object

objObjectThe object, or array, to iterate over.
fnFunctionThe function that will be executed on every object.

A generic iterator function, which can be used to seemlessly iterate over both objects and arrays. This function is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything.

The callback has two arguments:the key (objects) or index (arrays) as first the first, and the value as the second.

Examples

This is an example of iterating over the items in an array, accessing both the current item and its index.

Code
$.each( [0,1,2], function(i, n){
  alert( "Item #" + i + ": " + n );
});

This is an example of iterating over the properties in an Object, accessing both the current item and its key.

Code
$.each( { name: "John", lang: "JS" }, function(i, n){
  alert( "Name: " + i + ", Value: " + n );
});

$.trim(String str) : String

strStringThe string to trim.

Remove the whitespace from the beginning and end of a string.

Example

Code
$.trim("  hello, how are you?  ");
Result
"hello, how are you?"

$.merge(Array first, Array second) : Array

firstArrayThe first array to merge.
secondArrayThe second array to merge.

Merge two arrays together, removing all duplicates.

The new array is: All the results from the first array, followed by the unique results from the second array.

Examples

Merges two arrays, removing the duplicate 2

Code
$.merge( [0,1,2], [2,3,4] )
Result
[0,1,2,3,4]

Merges two arrays, removing the duplicates 3 and 2

Code
$.merge( [3,2,1], [4,3,2] )
Result
[3,2,1,4]

$.grep(Array array, Function fn, Boolean inv) : Array

arrayArrayThe Array to find items in.
fnFunctionThe function to process each item against.
invBooleanInvert the selection - select the opposite of the function.

Filter items out of an array, by using a filter function.

The specified function will be passed two arguments: The current array item and the index of the item in the array. The function must return 'true' to keep the item in the array, false to remove it.

Example

Code
$.grep( [0,1,2], function(i){
  return i > 0;
});
Result
[1, 2]

$.map(Array array, Function fn) : Array

arrayArrayThe Array to translate.
fnFunctionThe function to process each item against.

Translate all items in an array to another array of items.

The translation function that is provided to this method is called for each item in the array and is passed one argument: The item to be translated.

The function can then return the translated value, 'null' (to remove the item), or an array of values - which will be flattened into the full array.

Examples

Maps the original array to a new one and adds 4 to each value.

Code
$.map( [0,1,2], function(i){
  return i + 4;
});
Result
[4, 5, 6]

Maps the original array to a new one and adds 1 to each value if it is bigger then zero, otherwise it's removed-

Code
$.map( [0,1,2], function(i){
  return i > 0 ? i + 1 : null;
});
Result
[2, 3]

Maps the original array to a new one, each element is added with it's original value and the value plus one.

Code
$.map( [0,1,2], function(i){
  return [ i, i + 1 ];
});
Result
[0, 1, 1, 2, 2, 3]

$.browser() : Boolean

Contains flags for the useragent, read from navigator.userAgent. Available flags are: safari, opera, msie, mozilla

This property is available before the DOM is ready, therefore you can use it to add ready events only for certain browsers.

There are situations where object detections is not reliable enough, in that cases it makes sense to use browser detection. Simply try to avoid both!

A combination of browser and object detection yields quite reliable results.

Examples

Returns true if the current useragent is some version of microsoft's internet explorer

Code
$.browser.msie

Alerts "this is safari!" only for safari browsers

Code
if($.browser.safari) { $( function() { alert("this is safari!"); } ); }