Utility methods

jQuery has several utility methods available via jQuery or $. Don't be confused with the jQuery object $(sel).

var  html = $.parseHTML("hello, <b>my name is</b> jQuery.");

$.contains(document.documentElement, document.body); // true
$.contains(document.body, document.documentElement); // false

jQuery.globalEval("var newVar = true;");

var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
  xmlDoc = $.parseXML(xml),
  $xml = $(xmlDoc),
  $title = $xml.find("title");
method description
parseHTML(data [,context][,keepScripts])

Parses a string into an array of DOM nodes.

By default, the context parameter refers to the current document object.

Method does not run scripts in the parsed HTML unless keepScripts is explicitly true.

parseXML(str) Parses a well-formed XML string into an XML document.
contains(container, contained) Checks if a DOM element is a descendant of another DOM element.
each(array, callback) Allows to iterate over any collection, whether it is an object or an array. It is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object.
extend([deep], target, obj1 [,objN])

Merge the contents of two or more objects together into the target object.

If the deep is true, the merge becomes recursive (aka. deep copy). Passing false for this argument is not supported.

globalEval(code) Executes some JavaScript code globally. This method behaves differently from using a normal JavaScript eval() in that it's executed within the global context (which is important for loading external scripts dynamically).
grep( array, func [,invert])

Finds the elements of an array-like object which satisfy a filter function. The original object is not affected.

The funcis a function to process each item against.It returns boolean value and has two arguments:

  • elementOfArray - current item
  • indexInArray - index of current item from zero
The this refers to the the window object.
makeArray(obj) Converts an array-like object into a true JavaScript array.
merge( first, second ) Merges the contents of two array-like objects together into the first object.
map(array, cb)

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

The cbis a function, that returns new value and has two arguments:

  • elementOfArray - current element in array
  • indexInArray - index of current element from zero
The this refers to the the window object.
map(obj, cb)

Translate all items in object to new array of items.

The cbis a function, that returns new value and has two arguments:

  • propertyOfObject - property of object
  • key - key of the object property
The this refers to the the window object.
uniqueSort(array) Sorts an array of DOM elements, in place, with the duplicates removed. It can not be applied to the arrays of strings or numbers.
noop() An empty function. Can be used when you wish to pass a function that will do nothing.