2 * jQuery @VERSION - New Wave Javascript
4 * Copyright (c) 2007 John Resig (jquery.com)
5 * Dual licensed under the MIT (MIT-LICENSE.txt)
6 * and GPL (GPL-LICENSE.txt) licenses.
13 * Create a new jQuery Object
18 * @param String|Function|Element|Array<Element>|jQuery a selector
19 * @param jQuery|Element|Array<Element> c context
22 var jQuery = window.jQuery = function(a,c) {
23 // If the context is global, return a new object
24 if ( window == this || !this.init )
25 return new jQuery(a,c);
27 return this.init(a,c);
30 // Map over the $ in case of overwrite
31 if ( typeof $ != "undefined" )
34 // Map the jQuery namespace to the '$' one
38 * This function accepts a string containing a CSS or
39 * basic XPath selector which is then used to match a set of elements.
41 * The core functionality of jQuery centers around this function.
42 * Everything in jQuery is based upon this, or uses this in some way.
43 * The most basic use of this function is to pass in an expression
44 * (usually consisting of CSS or XPath), which then finds all matching
47 * By default, if no context is specified, $() looks for DOM elements within the context of the
48 * current HTML document. If you do specify a context, such as a DOM
49 * element or jQuery object, the expression will be matched against
50 * the contents of that context.
52 * See [[DOM/Traversing/Selectors]] for the allowed CSS/XPath syntax for expressions.
54 * @example $("div > p")
55 * @desc Finds all p elements that are children of a div element.
56 * @before <p>one</p> <div><p>two</p></div> <p>three</p>
57 * @result [ <p>two</p> ]
59 * @example $("input:radio", document.forms[0])
60 * @desc Searches for all inputs of type radio within the first form in the document
62 * @example $("div", xml.responseXML)
63 * @desc This finds all div elements within the specified XML document.
66 * @param String expr An expression to search with
67 * @param Element|jQuery context (optional) A DOM Element, Document or jQuery to use as context
71 * @see $(Element<Array>)
75 * Create DOM elements on-the-fly from the provided String of raw HTML.
77 * @example $("<div><p>Hello</p></div>").appendTo("body")
78 * @desc Creates a div element (and all of its contents) dynamically,
79 * and appends it to the body element. Internally, an
80 * element is created and its innerHTML property set to the given markup.
81 * It is therefore both quite flexible and limited.
84 * @param String html A string of HTML to create on the fly.
87 * @see appendTo(String)
91 * Wrap jQuery functionality around a single or multiple DOM Element(s).
93 * This function also accepts XML Documents and Window objects
94 * as valid arguments (even though they are not DOM Elements).
96 * @example $(document.body).css( "background", "black" );
97 * @desc Sets the background color of the page to black.
99 * @example $( myForm.elements ).hide()
100 * @desc Hides all the input elements within a form
103 * @param Element|Array<Element> elems DOM element(s) to be encapsulated by a jQuery object.
109 * A shorthand for $(document).ready(), allowing you to bind a function
110 * to be executed when the DOM document has finished loading. This function
111 * behaves just like $(document).ready(), in that it should be used to wrap
112 * other $() operations on your page that depend on the DOM being ready to be
113 * operated on. While this function is, technically, chainable - there really
114 * isn't much use for chaining against it.
116 * You can have as many $(document).ready events on your page as you like.
118 * See ready(Function) for details about the ready event.
120 * @example $(function(){
121 * // Document is ready
123 * @desc Executes the function when the DOM is ready to be used.
125 * @example jQuery(function($) {
126 * // Your code using failsafe $ alias here...
128 * @desc Uses both the shortcut for $(document).ready() and the argument
129 * to write failsafe jQuery code using the $ alias, without relying on the
133 * @param Function fn The function to execute when the DOM is ready.
136 * @see ready(Function)
139 jQuery.fn = jQuery.prototype = {
141 * Initialize a new jQuery object
145 * @param String|Function|Element|Array<Element>|jQuery a selector
146 * @param jQuery|Element|Array<Element> c context
149 init: function(a,c) {
150 // Make sure that a selection was provided
153 // HANDLE: $(function)
154 // Shortcut for document ready
155 if ( jQuery.isFunction(a) )
156 return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a );
158 // Handle HTML strings
159 if ( typeof a == "string" ) {
160 // HANDLE: $(html) -> $(array)
161 var m = /^[^<]*(<(.|\s)+>)[^>]*$/.exec(a);
163 a = jQuery.clean( [ m[1] ] );
167 return new jQuery( c ).find( a );
170 return this.setArray(
172 a.constructor == Array && a ||
174 // HANDLE: $(arraylike)
175 // Watch for when an array-like object is passed as the selector
176 (a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray( a ) ||
183 * The current version of jQuery.
194 * The number of elements currently matched. The size function will return the same value.
196 * @example $("img").length;
197 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
207 * Get the number of elements currently matched. This returns the same
208 * number as the 'length' property of the jQuery object.
210 * @example $("img").size();
211 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
225 * Access all matched DOM elements. This serves as a backwards-compatible
226 * way of accessing all matched elements (other than the jQuery object
227 * itself, which is, in fact, an array of elements).
229 * It is useful if you need to operate on the DOM elements themselves instead of using built-in jQuery functions.
231 * @example $("img").get();
232 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
233 * @result [ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
234 * @desc Selects all images in the document and returns the DOM Elements as an Array
237 * @type Array<Element>
242 * Access a single matched DOM element at a specified index in the matched set.
243 * This allows you to extract the actual DOM element and operate on it
244 * directly without necessarily using jQuery functionality on it.
246 * @example $("img").get(0);
247 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
248 * @result <img src="test1.jpg"/>
249 * @desc Selects all images in the document and returns the first one
253 * @param Number num Access the element in the Nth position.
256 get: function( num ) {
257 return num == undefined ?
259 // Return a 'clean' array
260 jQuery.makeArray( this ) :
262 // Return just the object
267 * Set the jQuery object to an array of elements, while maintaining
270 * @example $("img").pushStack([ document.body ]);
271 * @result $("img").pushStack() == [ document.body ]
276 * @param Elements elems An array of elements
279 pushStack: function( a ) {
281 ret.prevObject = this;
286 * Set the jQuery object to an array of elements. This operation is
287 * completely destructive - be sure to use .pushStack() if you wish to maintain
290 * @example $("img").setArray([ document.body ]);
291 * @result $("img").setArray() == [ document.body ]
296 * @param Elements elems An array of elements
299 setArray: function( a ) {
301 Array.prototype.push.apply( this, a );
306 * Execute a function within the context of every matched element.
307 * This means that every time the passed-in function is executed
308 * (which is once for every element matched) the 'this' keyword
309 * points to the specific DOM element.
311 * Additionally, the function, when executed, is passed a single
312 * argument representing the position of the element in the matched
313 * set (integer, zero-index).
315 * @example $("img").each(function(i){
316 * this.src = "test" + i + ".jpg";
318 * @before <img/><img/>
319 * @result <img src="test0.jpg"/><img src="test1.jpg"/>
320 * @desc Iterates over two images and sets their src property
324 * @param Function fn A function to execute
327 each: function( fn, args ) {
328 return jQuery.each( this, fn, args );
332 * Searches every matched element for the object and returns
333 * the index of the element, if found, starting with zero.
334 * Returns -1 if the object wasn't found.
336 * @example $("*").index( $('#foobar')[0] )
337 * @before <div id="foobar"><b></b><span id="foo"></span></div>
339 * @desc Returns the index for the element with ID foobar
341 * @example $("*").index( $('#foo')[0] )
342 * @before <div id="foobar"><b></b><span id="foo"></span></div>
344 * @desc Returns the index for the element with ID foo within another element
346 * @example $("*").index( $('#bar')[0] )
347 * @before <div id="foobar"><b></b><span id="foo"></span></div>
349 * @desc Returns -1, as there is no element with ID bar
353 * @param Element subject Object to search for
356 index: function( obj ) {
358 this.each(function(i){
359 if ( this == obj ) pos = i;
365 * Access a property on the first matched element.
366 * This method makes it easy to retrieve a property value
367 * from the first matched element.
369 * If the element does not have an attribute with such a
370 * name, undefined is returned.
372 * @example $("img").attr("src");
373 * @before <img src="test.jpg"/>
375 * @desc Returns the src attribute from the first image in the document.
379 * @param String name The name of the property to access.
380 * @cat DOM/Attributes
384 * Set a key/value object as properties to all matched elements.
386 * This serves as the best way to set a large number of properties
387 * on all matched elements.
389 * @example $("img").attr({ src: "test.jpg", alt: "Test Image" });
391 * @result <img src="test.jpg" alt="Test Image"/>
392 * @desc Sets src and alt attributes to all images.
396 * @param Map properties Key/value pairs to set as object properties.
397 * @cat DOM/Attributes
401 * Set a single property to a value, on all matched elements.
403 * Note that you can't set the name property of input elements in IE.
404 * Use $(html) or .append(html) or .html(html) to create elements
405 * on the fly including the name property.
407 * @example $("img").attr("src","test.jpg");
409 * @result <img src="test.jpg"/>
410 * @desc Sets src attribute to all images.
414 * @param String key The name of the property to set.
415 * @param Object value The value to set the property to.
416 * @cat DOM/Attributes
420 * Set a single property to a computed value, on all matched elements.
422 * Instead of supplying a string value as described
423 * [[DOM/Attributes#attr.28_key.2C_value_.29|above]],
424 * a function is provided that computes the value.
426 * @example $("img").attr("title", function() { return this.src });
427 * @before <img src="test.jpg" />
428 * @result <img src="test.jpg" title="test.jpg" />
429 * @desc Sets title attribute from src attribute.
431 * @example $("img").attr("title", function(index) { return this.title + (i + 1); });
432 * @before <img title="pic" /><img title="pic" /><img title="pic" />
433 * @result <img title="pic1" /><img title="pic2" /><img title="pic3" />
434 * @desc Enumerate title attribute.
438 * @param String key The name of the property to set.
439 * @param Function value A function returning the value to set.
440 * Scope: Current element, argument: Index of current element
441 * @cat DOM/Attributes
443 attr: function( key, value, type ) {
446 // Look for the case where we're accessing a style value
447 if ( key.constructor == String )
448 if ( value == undefined )
449 return this.length && jQuery[ type || "attr" ]( this[0], key ) || undefined;
455 // Check to see if we're setting style values
456 return this.each(function(index){
457 // Set all the styles
458 for ( var prop in obj )
460 type ? this.style : this,
461 prop, jQuery.prop(this, obj[prop], type, index, prop)
467 * Access a style property on the first matched element.
468 * This method makes it easy to retrieve a style property value
469 * from the first matched element.
471 * @example $("p").css("color");
472 * @before <p style="color:red;">Test Paragraph.</p>
474 * @desc Retrieves the color style of the first paragraph
476 * @example $("p").css("font-weight");
477 * @before <p style="font-weight: bold;">Test Paragraph.</p>
479 * @desc Retrieves the font-weight style of the first paragraph.
483 * @param String name The name of the property to access.
488 * Set a key/value object as style properties to all matched elements.
490 * This serves as the best way to set a large number of style properties
491 * on all matched elements.
493 * @example $("p").css({ color: "red", background: "blue" });
494 * @before <p>Test Paragraph.</p>
495 * @result <p style="color:red; background:blue;">Test Paragraph.</p>
496 * @desc Sets color and background styles to all p elements.
500 * @param Map properties Key/value pairs to set as style properties.
505 * Set a single style property to a value, on all matched elements.
506 * If a number is provided, it is automatically converted into a pixel value.
508 * @example $("p").css("color","red");
509 * @before <p>Test Paragraph.</p>
510 * @result <p style="color:red;">Test Paragraph.</p>
511 * @desc Changes the color of all paragraphs to red
513 * @example $("p").css("left",30);
514 * @before <p>Test Paragraph.</p>
515 * @result <p style="left:30px;">Test Paragraph.</p>
516 * @desc Changes the left of all paragraphs to "30px"
520 * @param String key The name of the property to set.
521 * @param String|Number value The value to set the property to.
524 css: function( key, value ) {
525 return this.attr( key, value, "curCSS" );
529 * Get the text contents of all matched elements. The result is
530 * a string that contains the combined text contents of all matched
531 * elements. This method works on both HTML and XML documents.
533 * @example $("p").text();
534 * @before <p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>
535 * @result Test Paragraph.Paraparagraph
536 * @desc Gets the concatenated text of all paragraphs
540 * @cat DOM/Attributes
544 * Set the text contents of all matched elements.
546 * Similar to html(), but escapes HTML (replace "<" and ">" with their
549 * @example $("p").text("<b>Some</b> new text.");
550 * @before <p>Test Paragraph.</p>
551 * @result <p><b>Some</b> new text.</p>
552 * @desc Sets the text of all paragraphs.
554 * @example $("p").text("<b>Some</b> new text.", true);
555 * @before <p>Test Paragraph.</p>
556 * @result <p>Some new text.</p>
557 * @desc Sets the text of all paragraphs.
561 * @param String val The text value to set the contents of the element to.
562 * @cat DOM/Attributes
565 if ( typeof e != "object" && e != null )
566 return this.empty().append( document.createTextNode( e ) );
569 jQuery.each( e || this, function(){
570 jQuery.each( this.childNodes, function(){
571 if ( this.nodeType != 8 )
572 t += this.nodeType != 1 ?
573 this.nodeValue : jQuery.fn.text([ this ]);
580 * Wrap all matched elements with a structure of other elements.
581 * This wrapping process is most useful for injecting additional
582 * stucture into a document, without ruining the original semantic
583 * qualities of a document.
585 * This works by going through the first element
586 * provided (which is generated, on the fly, from the provided HTML)
587 * and finds the deepest ancestor element within its
588 * structure - it is that element that will en-wrap everything else.
590 * This does not work with elements that contain text. Any necessary text
591 * must be added after the wrapping is done.
593 * @example $("p").wrap("<div class='wrap'></div>");
594 * @before <p>Test Paragraph.</p>
595 * @result <div class='wrap'><p>Test Paragraph.</p></div>
599 * @param String html A string of HTML, that will be created on the fly and wrapped around the target.
600 * @cat DOM/Manipulation
604 * Wrap all matched elements with a structure of other elements.
605 * This wrapping process is most useful for injecting additional
606 * stucture into a document, without ruining the original semantic
607 * qualities of a document.
609 * This works by going through the first element
610 * provided and finding the deepest ancestor element within its
611 * structure - it is that element that will en-wrap everything else.
613 * This does not work with elements that contain text. Any necessary text
614 * must be added after the wrapping is done.
616 * @example $("p").wrap( document.getElementById('content') );
617 * @before <p>Test Paragraph.</p><div id="content"></div>
618 * @result <div id="content"><p>Test Paragraph.</p></div>
622 * @param Element elem A DOM element that will be wrapped around the target.
623 * @cat DOM/Manipulation
626 // The elements to wrap the target around
627 var a, args = arguments;
629 // Wrap each of the matched elements individually
630 return this.each(function(){
632 a = jQuery.clean(args, this.ownerDocument);
634 // Clone the structure that we're using to wrap
635 var b = a[0].cloneNode(true);
637 // Insert it before the element to be wrapped
638 this.parentNode.insertBefore( b, this );
640 // Find the deepest point in the wrap structure
641 while ( b.firstChild )
644 // Move the matched element to within the wrap structure
645 b.appendChild( this );
650 * Append content to the inside of every matched element.
652 * This operation is similar to doing an appendChild to all the
653 * specified elements, adding them into the document.
655 * @example $("p").append("<b>Hello</b>");
656 * @before <p>I would like to say: </p>
657 * @result <p>I would like to say: <b>Hello</b></p>
658 * @desc Appends some HTML to all paragraphs.
660 * @example $("p").append( $("#foo")[0] );
661 * @before <p>I would like to say: </p><b id="foo">Hello</b>
662 * @result <p>I would like to say: <b id="foo">Hello</b></p>
663 * @desc Appends an Element to all paragraphs.
665 * @example $("p").append( $("b") );
666 * @before <p>I would like to say: </p><b>Hello</b>
667 * @result <p>I would like to say: <b>Hello</b></p>
668 * @desc Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
672 * @param <Content> content Content to append to the target
673 * @cat DOM/Manipulation
674 * @see prepend(<Content>)
675 * @see before(<Content>)
676 * @see after(<Content>)
679 return this.domManip(arguments, true, 1, function(a){
680 this.appendChild( a );
685 * Prepend content to the inside of every matched element.
687 * This operation is the best way to insert elements
688 * inside, at the beginning, of all matched elements.
690 * @example $("p").prepend("<b>Hello</b>");
691 * @before <p>I would like to say: </p>
692 * @result <p><b>Hello</b>I would like to say: </p>
693 * @desc Prepends some HTML to all paragraphs.
695 * @example $("p").prepend( $("#foo")[0] );
696 * @before <p>I would like to say: </p><b id="foo">Hello</b>
697 * @result <p><b id="foo">Hello</b>I would like to say: </p>
698 * @desc Prepends an Element to all paragraphs.
700 * @example $("p").prepend( $("b") );
701 * @before <p>I would like to say: </p><b>Hello</b>
702 * @result <p><b>Hello</b>I would like to say: </p>
703 * @desc Prepends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
707 * @param <Content> content Content to prepend to the target.
708 * @cat DOM/Manipulation
709 * @see append(<Content>)
710 * @see before(<Content>)
711 * @see after(<Content>)
713 prepend: function() {
714 return this.domManip(arguments, true, -1, function(a){
715 this.insertBefore( a, this.firstChild );
720 * Insert content before each of the matched elements.
722 * @example $("p").before("<b>Hello</b>");
723 * @before <p>I would like to say: </p>
724 * @result <b>Hello</b><p>I would like to say: </p>
725 * @desc Inserts some HTML before all paragraphs.
727 * @example $("p").before( $("#foo")[0] );
728 * @before <p>I would like to say: </p><b id="foo">Hello</b>
729 * @result <b id="foo">Hello</b><p>I would like to say: </p>
730 * @desc Inserts an Element before all paragraphs.
732 * @example $("p").before( $("b") );
733 * @before <p>I would like to say: </p><b>Hello</b>
734 * @result <b>Hello</b><p>I would like to say: </p>
735 * @desc Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs.
739 * @param <Content> content Content to insert before each target.
740 * @cat DOM/Manipulation
741 * @see append(<Content>)
742 * @see prepend(<Content>)
743 * @see after(<Content>)
746 return this.domManip(arguments, false, 1, function(a){
747 this.parentNode.insertBefore( a, this );
752 * Insert content after each of the matched elements.
754 * @example $("p").after("<b>Hello</b>");
755 * @before <p>I would like to say: </p>
756 * @result <p>I would like to say: </p><b>Hello</b>
757 * @desc Inserts some HTML after all paragraphs.
759 * @example $("p").after( $("#foo")[0] );
760 * @before <b id="foo">Hello</b><p>I would like to say: </p>
761 * @result <p>I would like to say: </p><b id="foo">Hello</b>
762 * @desc Inserts an Element after all paragraphs.
764 * @example $("p").after( $("b") );
765 * @before <b>Hello</b><p>I would like to say: </p>
766 * @result <p>I would like to say: </p><b>Hello</b>
767 * @desc Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs.
771 * @param <Content> content Content to insert after each target.
772 * @cat DOM/Manipulation
773 * @see append(<Content>)
774 * @see prepend(<Content>)
775 * @see before(<Content>)
778 return this.domManip(arguments, false, -1, function(a){
779 this.parentNode.insertBefore( a, this.nextSibling );
784 * Revert the most recent 'destructive' operation, changing the set of matched elements
785 * to its previous state (right before the destructive operation).
787 * If there was no destructive operation before, an empty set is returned.
789 * A 'destructive' operation is any operation that changes the set of
790 * matched jQuery elements. These functions are: <code>add</code>,
791 * <code>children</code>, <code>clone</code>, <code>filter</code>,
792 * <code>find</code>, <code>not</code>, <code>next</code>,
793 * <code>parent</code>, <code>parents</code>, <code>prev</code> and <code>siblings</code>.
795 * @example $("p").find("span").end();
796 * @before <p><span>Hello</span>, how are you?</p>
797 * @result [ <p>...</p> ]
798 * @desc Selects all paragraphs, finds span elements inside these, and reverts the
799 * selection back to the paragraphs.
803 * @cat DOM/Traversing
806 return this.prevObject || jQuery([]);
810 * Searches for all elements that match the specified expression.
812 * This method is a good way to find additional descendant
813 * elements with which to process.
815 * All searching is done using a jQuery expression. The expression can be
816 * written using CSS 1-3 Selector syntax, or basic XPath.
818 * @example $("p").find("span");
819 * @before <p><span>Hello</span>, how are you?</p>
820 * @result [ <span>Hello</span> ]
821 * @desc Starts with all paragraphs and searches for descendant span
822 * elements, same as $("p span")
826 * @param String expr An expression to search with.
827 * @cat DOM/Traversing
830 var data = jQuery.map(this, function(a){ return jQuery.find(t,a); });
831 return this.pushStack( /[^+>] [^+>]/.test( t ) || t.indexOf("..") > -1 ?
832 jQuery.unique( data ) : data );
836 * Clone matched DOM Elements and select the clones.
838 * This is useful for moving copies of the elements to another
839 * location in the DOM.
841 * @example $("b").clone().prependTo("p");
842 * @before <b>Hello</b><p>, how are you?</p>
843 * @result <b>Hello</b><p><b>Hello</b>, how are you?</p>
844 * @desc Clones all b elements (and selects the clones) and prepends them to all paragraphs.
848 * @param Boolean deep (Optional) Set to false if you don't want to clone all descendant nodes, in addition to the element itself.
849 * @cat DOM/Manipulation
851 clone: function(deep) {
852 deep = deep != undefined ? deep : true;
853 var $this = this.add(this.find("*"));
854 if (jQuery.browser.msie) {
855 // Need to remove events on the element and its descendants
856 $this.each(function() {
858 for (var type in this.$events)
859 this._$events[type] = jQuery.extend({},this.$events[type]);
864 var r = this.pushStack( jQuery.map( this, function(a){
865 return a.cloneNode( deep );
868 if (jQuery.browser.msie) {
869 $this.each(function() {
870 // Add the events back to the original and its descendants
871 var events = this._$events;
872 for (var type in events)
873 for (var handler in events[type])
874 jQuery.event.add(this, type, events[type][handler], events[type][handler].data);
875 this._$events = null;
879 // copy form values over
881 var inputs = r.add(r.find('*')).filter('select,input[@type=checkbox]');
882 $this.filter('select,input[@type=checkbox]').each(function(i) {
883 if (this.selectedIndex)
884 inputs[i].selectedIndex = this.selectedIndex;
886 inputs[i].checked = true;
890 // Return the cloned set
895 * Removes all elements from the set of matched elements that do not
896 * match the specified expression(s). This method is used to narrow down
897 * the results of a search.
899 * Provide a comma-separated list of expressions to apply multiple filters at once.
901 * @example $("p").filter(".selected")
902 * @before <p class="selected">Hello</p><p>How are you?</p>
903 * @result [ <p class="selected">Hello</p> ]
904 * @desc Selects all paragraphs and removes those without a class "selected".
906 * @example $("p").filter(".selected, :first")
907 * @before <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
908 * @result [ <p>Hello</p>, <p class="selected">And Again</p> ]
909 * @desc Selects all paragraphs and removes those without class "selected" and being the first one.
913 * @param String expression Expression(s) to search with.
914 * @cat DOM/Traversing
918 * Removes all elements from the set of matched elements that do not
919 * pass the specified filter. This method is used to narrow down
920 * the results of a search.
922 * @example $("p").filter(function(index) {
923 * return $("ol", this).length == 0;
925 * @before <p><ol><li>Hello</li></ol></p><p>How are you?</p>
926 * @result [ <p>How are you?</p> ]
927 * @desc Remove all elements that have a child ol element
931 * @param Function filter A function to use for filtering
932 * @cat DOM/Traversing
934 filter: function(t) {
935 return this.pushStack(
936 jQuery.isFunction( t ) &&
937 jQuery.grep(this, function(el, index){
938 return t.apply(el, [index]);
941 jQuery.multiFilter(t,this) );
945 * Removes the specified Element from the set of matched elements. This
946 * method is used to remove a single Element from a jQuery object.
948 * @example $("p").not( $("#selected")[0] )
949 * @before <p>Hello</p><p id="selected">Hello Again</p>
950 * @result [ <p>Hello</p> ]
951 * @desc Removes the element with the ID "selected" from the set of all paragraphs.
955 * @param Element el An element to remove from the set
956 * @cat DOM/Traversing
960 * Removes elements matching the specified expression from the set
961 * of matched elements. This method is used to remove one or more
962 * elements from a jQuery object.
964 * @example $("p").not("#selected")
965 * @before <p>Hello</p><p id="selected">Hello Again</p>
966 * @result [ <p>Hello</p> ]
967 * @desc Removes the element with the ID "selected" from the set of all paragraphs.
971 * @param String expr An expression with which to remove matching elements
972 * @cat DOM/Traversing
976 * Removes any elements inside the array of elements from the set
977 * of matched elements. This method is used to remove one or more
978 * elements from a jQuery object.
980 * Please note: the expression cannot use a reference to the
981 * element name. See the two examples below.
983 * @example $("p").not( $("div p.selected") )
984 * @before <div><p>Hello</p><p class="selected">Hello Again</p></div>
985 * @result [ <p>Hello</p> ]
986 * @desc Removes all elements that match "div p.selected" from the total set of all paragraphs.
990 * @param jQuery elems A set of elements to remove from the jQuery set of matched elements.
991 * @cat DOM/Traversing
994 return this.pushStack(
995 t.constructor == String &&
996 jQuery.multiFilter(t, this, true) ||
998 jQuery.grep(this, function(a) {
999 return ( t.constructor == Array || t.jquery )
1000 ? jQuery.inArray( a, t ) < 0
1007 * Adds more elements, matched by the given expression,
1008 * to the set of matched elements.
1010 * @example $("p").add("span")
1011 * @before (HTML) <p>Hello</p><span>Hello Again</span>
1012 * @result (jQuery object matching 2 elements) [ <p>Hello</p>, <span>Hello Again</span> ]
1013 * @desc Compare the above result to the result of <code>$('p')</code>,
1014 * which would just result in <code><nowiki>[ <p>Hello</p> ]</nowiki></code>.
1015 * Using add(), matched elements of <code>$('span')</code> are simply
1016 * added to the returned jQuery-object.
1020 * @param String expr An expression whose matched elements are added
1021 * @cat DOM/Traversing
1025 * Adds more elements, created on the fly, to the set of
1028 * @example $("p").add("<span>Again</span>")
1029 * @before <p>Hello</p>
1030 * @result [ <p>Hello</p>, <span>Again</span> ]
1034 * @param String html A string of HTML to create on the fly.
1035 * @cat DOM/Traversing
1039 * Adds one or more Elements to the set of matched elements.
1041 * @example $("p").add( document.getElementById("a") )
1042 * @before <p>Hello</p><p><span id="a">Hello Again</span></p>
1043 * @result [ <p>Hello</p>, <span id="a">Hello Again</span> ]
1045 * @example $("p").add( document.forms[0].elements )
1046 * @before <p>Hello</p><p><form><input/><button/></form>
1047 * @result [ <p>Hello</p>, <input/>, <button/> ]
1051 * @param Element|Array<Element> elements One or more Elements to add
1052 * @cat DOM/Traversing
1055 return this.pushStack( jQuery.merge(
1057 t.constructor == String ?
1059 t.length != undefined && (!t.nodeName || t.nodeName == "FORM") ?
1065 * Checks the current selection against an expression and returns true,
1066 * if at least one element of the selection fits the given expression.
1068 * Does return false, if no element fits or the expression is not valid.
1070 * filter(String) is used internally, therefore all rules that apply there
1073 * @example $("input[@type='checkbox']").parent().is("form")
1074 * @before <form><input type="checkbox" /></form>
1076 * @desc Returns true, because the parent of the input is a form element
1078 * @example $("input[@type='checkbox']").parent().is("form")
1079 * @before <form><p><input type="checkbox" /></p></form>
1081 * @desc Returns false, because the parent of the input is a p element
1085 * @param String expr The expression with which to filter
1086 * @cat DOM/Traversing
1088 is: function(expr) {
1089 return expr ? jQuery.multiFilter(expr,this).length > 0 : false;
1093 * Get the content of the value attribute of the first matched element.
1095 * Use caution when relying on this function to check the value of
1096 * multiple-select elements and checkboxes in a form. While it will
1097 * still work as intended, it may not accurately represent the value
1098 * the server will receive because these elements may send an array
1099 * of values. For more robust handling of field values, see the
1100 * [http://www.malsup.com/jquery/form/#fields fieldValue function of the Form Plugin].
1102 * @example $("input").val();
1103 * @before <input type="text" value="some text"/>
1104 * @result "some text"
1108 * @cat DOM/Attributes
1112 * Set the value attribute of every matched element.
1114 * @example $("input").val("test");
1115 * @before <input type="text" value="some text"/>
1116 * @result <input type="text" value="test"/>
1120 * @param String val Set the property to the specified value.
1121 * @cat DOM/Attributes
1123 val: function( val ) {
1124 return val == undefined ?
1125 ( this.length ? this[0].value : null ) :
1126 this.attr( "value", val );
1130 * Get the html contents of the first matched element.
1131 * This property is not available on XML documents.
1133 * @example $("div").html();
1134 * @before <div><input/></div>
1139 * @cat DOM/Attributes
1143 * Set the html contents of every matched element.
1144 * This property is not available on XML documents.
1146 * @example $("div").html("<b>new stuff</b>");
1147 * @before <div><input/></div>
1148 * @result <div><b>new stuff</b></div>
1152 * @param String val Set the html contents to the specified value.
1153 * @cat DOM/Attributes
1155 html: function( val ) {
1156 return val == undefined ?
1157 ( this.length ? this[0].innerHTML : null ) :
1158 this.empty().append( val );
1162 return this.pushStack( Array.prototype.slice.apply( this, arguments ) );
1169 * @param Boolean table Insert TBODY in TABLEs if one is not found.
1170 * @param Number dir If dir<0, process args in reverse order.
1171 * @param Function fn The function doing the DOM manipulation.
1175 domManip: function(args, table, dir, fn){
1176 var clone = this.length > 1, a;
1178 return this.each(function(){
1180 a = jQuery.clean(args, this.ownerDocument);
1187 if ( table && jQuery.nodeName(this, "table") && jQuery.nodeName(a[0], "tr") )
1188 obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));
1190 jQuery.each( a, function(){
1191 if ( jQuery.nodeName(this, "script") ) {
1193 jQuery.ajax({ url: this.src, async: false, dataType: "script" });
1195 jQuery.globalEval( this.text || this.textContent || this.innerHTML || "" );
1197 fn.apply( obj, [ clone ? this.cloneNode(true) : this ] );
1204 * Extends the jQuery object itself. Can be used to add functions into
1205 * the jQuery namespace and to [[Plugins/Authoring|add plugin methods]] (plugins).
1207 * @example jQuery.fn.extend({
1208 * check: function() {
1209 * return this.each(function() { this.checked = true; });
1211 * uncheck: function() {
1212 * return this.each(function() { this.checked = false; });
1215 * $("input[@type=checkbox]").check();
1216 * $("input[@type=radio]").uncheck();
1217 * @desc Adds two plugin methods.
1219 * @example jQuery.extend({
1220 * min: function(a, b) { return a < b ? a : b; },
1221 * max: function(a, b) { return a > b ? a : b; }
1223 * @desc Adds two functions into the jQuery namespace
1226 * @param Object prop The object that will be merged into the jQuery object
1232 * Extend one object with one or more others, returning the original,
1233 * modified, object. This is a great utility for simple inheritance.
1235 * @example var settings = { validate: false, limit: 5, name: "foo" };
1236 * var options = { validate: true, name: "bar" };
1237 * jQuery.extend(settings, options);
1238 * @result settings == { validate: true, limit: 5, name: "bar" }
1239 * @desc Merge settings and options, modifying settings
1241 * @example var defaults = { validate: false, limit: 5, name: "foo" };
1242 * var options = { validate: true, name: "bar" };
1243 * var settings = jQuery.extend({}, defaults, options);
1244 * @result settings == { validate: true, limit: 5, name: "bar" }
1245 * @desc Merge defaults and options, without modifying the defaults
1248 * @param Object target The object to extend
1249 * @param Object prop1 The object that will be merged into the first.
1250 * @param Object propN (optional) More objects to merge into the first
1254 jQuery.extend = jQuery.fn.extend = function() {
1255 // copy reference to target object
1256 var target = arguments[0] || {}, a = 1, al = arguments.length, deep = false;
1258 // Handle a deep copy situation
1259 if ( target.constructor == Boolean ) {
1261 target = arguments[1] || {};
1264 // extend jQuery itself if only one argument is passed
1272 for ( ; a < al; a++ )
1273 // Only deal with non-null/undefined values
1274 if ( (prop = arguments[a]) != null )
1275 // Extend the base object
1276 for ( var i in prop ) {
1277 // Prevent never-ending loop
1278 if ( target == prop[i] )
1281 // Recurse if we're merging object values
1282 if ( deep && typeof prop[i] == 'object' && target[i] )
1283 jQuery.extend( target[i], prop[i] );
1285 // Don't bring in undefined values
1286 else if ( prop[i] != undefined )
1287 target[i] = prop[i];
1290 // Return the modified object
1296 * Run this function to give control of the $ variable back
1297 * to whichever library first implemented it. This helps to make
1298 * sure that jQuery doesn't conflict with the $ object
1299 * of other libraries.
1301 * By using this function, you will only be able to access jQuery
1302 * using the 'jQuery' variable. For example, where you used to do
1303 * $("div p"), you now must do jQuery("div p").
1305 * @example jQuery.noConflict();
1306 * // Do something with jQuery
1307 * jQuery("div p").hide();
1308 * // Do something with another library's $()
1309 * $("content").style.display = 'none';
1310 * @desc Maps the original object that was referenced by $ back to $
1312 * @example jQuery.noConflict();
1315 * // more code using $ as alias to jQuery
1318 * // other code using $ as an alias to the other library
1319 * @desc Reverts the $ alias and then creates and executes a
1320 * function to provide the $ as a jQuery alias inside the functions
1321 * scope. Inside the function the original $ object is not available.
1322 * This works well for most plugins that don't rely on any other library.
1325 * @name $.noConflict
1329 noConflict: function() {
1335 // This may seem like some crazy code, but trust me when I say that this
1336 // is the only cross-browser way to do this. --John
1337 isFunction: function( fn ) {
1338 return !!fn && typeof fn != "string" && !fn.nodeName &&
1339 fn.constructor != Array && /function/i.test( fn + "" );
1342 // check if an element is in a XML document
1343 isXMLDoc: function(elem) {
1344 return elem.documentElement && !elem.body ||
1345 elem.tagName && elem.ownerDocument && !elem.ownerDocument.body;
1348 // Evalulates a script in a global context
1349 // Evaluates Async. in Safari 2 :-(
1350 globalEval: function( data ) {
1351 data = jQuery.trim( data );
1353 if ( window.execScript )
1354 window.execScript( data );
1355 else if ( jQuery.browser.safari )
1356 // safari doesn't provide a synchronous global eval
1357 window.setTimeout( data, 0 );
1359 eval.call( window, data );
1363 nodeName: function( elem, name ) {
1364 return elem.nodeName && elem.nodeName.toUpperCase() == name.toUpperCase();
1368 * A generic iterator function, which can be used to seamlessly
1369 * iterate over both objects and arrays. This function is not the same
1370 * as $().each() - which is used to iterate, exclusively, over a jQuery
1371 * object. This function can be used to iterate over anything.
1373 * The callback has two arguments:the key (objects) or index (arrays) as first
1374 * the first, and the value as the second.
1376 * @example $.each( [0,1,2], function(i, n){
1377 * alert( "Item #" + i + ": " + n );
1379 * @desc This is an example of iterating over the items in an array,
1380 * accessing both the current item and its index.
1382 * @example $.each( { name: "John", lang: "JS" }, function(i, n){
1383 * alert( "Name: " + i + ", Value: " + n );
1386 * @desc This is an example of iterating over the properties in an
1387 * Object, accessing both the current item and its key.
1390 * @param Object obj The object, or array, to iterate over.
1391 * @param Function fn The function that will be executed on every object.
1395 // args is for internal usage only
1396 each: function( obj, fn, args ) {
1398 if ( obj.length == undefined )
1399 for ( var i in obj )
1400 fn.apply( obj[i], args );
1402 for ( var i = 0, ol = obj.length; i < ol; i++ )
1403 if ( fn.apply( obj[i], args ) === false ) break;
1405 // A special, fast, case for the most common use of each
1407 if ( obj.length == undefined )
1408 for ( var i in obj )
1409 fn.call( obj[i], i, obj[i] );
1411 for ( var i = 0, ol = obj.length, val = obj[0];
1412 i < ol && fn.call(val,i,val) !== false; val = obj[++i] );
1418 prop: function(elem, value, type, index, prop){
1419 // Handle executable functions
1420 if ( jQuery.isFunction( value ) )
1421 value = value.call( elem, [index] );
1423 // exclude the following css properties to add px
1424 var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i;
1426 // Handle passing in a number to a CSS property
1427 return value && value.constructor == Number && type == "curCSS" && !exclude.test(prop) ?
1433 // internal only, use addClass("class")
1434 add: function( elem, c ){
1435 jQuery.each( (c || "").split(/\s+/), function(i, cur){
1436 if ( !jQuery.className.has( elem.className, cur ) )
1437 elem.className += ( elem.className ? " " : "" ) + cur;
1441 // internal only, use removeClass("class")
1442 remove: function( elem, c ){
1443 elem.className = c != undefined ?
1444 jQuery.grep( elem.className.split(/\s+/), function(cur){
1445 return !jQuery.className.has( c, cur );
1449 // internal only, use is(".class")
1450 has: function( t, c ) {
1451 return jQuery.inArray( c, (t.className || t).toString().split(/\s+/) ) > -1;
1456 * Swap in/out style options.
1459 swap: function(e,o,f) {
1460 for ( var i in o ) {
1461 e.style["old"+i] = e.style[i];
1466 e.style[i] = e.style["old"+i];
1469 css: function(e,p) {
1470 if ( p == "height" || p == "width" ) {
1471 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
1473 jQuery.each( d, function(){
1474 old["padding" + this] = 0;
1475 old["border" + this + "Width"] = 0;
1478 jQuery.swap( e, old, function() {
1479 if ( jQuery(e).is(':visible') ) {
1480 oHeight = e.offsetHeight;
1481 oWidth = e.offsetWidth;
1483 e = jQuery(e.cloneNode(true))
1484 .find(":radio").removeAttr("checked").end()
1486 visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
1487 }).appendTo(e.parentNode)[0];
1489 var parPos = jQuery.css(e.parentNode,"position") || "static";
1490 if ( parPos == "static" )
1491 e.parentNode.style.position = "relative";
1493 oHeight = e.clientHeight;
1494 oWidth = e.clientWidth;
1496 if ( parPos == "static" )
1497 e.parentNode.style.position = "static";
1499 e.parentNode.removeChild(e);
1503 return p == "height" ? oHeight : oWidth;
1506 return jQuery.curCSS( e, p );
1509 curCSS: function(elem, prop, force) {
1510 var ret, stack = [], swap = [];
1512 // A helper method for determining if an element's values are broken
1514 if ( !jQuery.browser.safari )
1517 var ret = document.defaultView.getComputedStyle(a,null);
1518 return !ret || ret.getPropertyValue("color") == "";
1521 if (prop == "opacity" && jQuery.browser.msie) {
1522 ret = jQuery.attr(elem.style, "opacity");
1523 return ret == "" ? "1" : ret;
1526 if (prop.match(/float/i))
1529 if (!force && elem.style[prop])
1530 ret = elem.style[prop];
1532 else if (document.defaultView && document.defaultView.getComputedStyle) {
1534 if (prop.match(/float/i))
1537 prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();
1538 var cur = document.defaultView.getComputedStyle(elem, null);
1540 if ( cur && !color(elem) )
1541 ret = cur.getPropertyValue(prop);
1543 // If the element isn't reporting its values properly in Safari
1544 // then some display: none elements are involved
1546 // Locate all of the parent display: none elements
1547 for ( var a = elem; a && color(a); a = a.parentNode )
1550 // Go through and make them visible, but in reverse
1551 // (It would be better if we knew the exact display type that they had)
1552 for ( a = 0; a < stack.length; a++ )
1553 if ( color(stack[a]) ) {
1554 swap[a] = stack[a].style.display;
1555 stack[a].style.display = "block";
1558 // Since we flip the display style, we have to handle that
1559 // one special, otherwise get the value
1560 ret = prop == "display" && swap[stack.length-1] != null ?
1562 document.defaultView.getComputedStyle(elem,null).getPropertyValue(prop) || "";
1564 // Finally, revert the display styles back
1565 for ( a = 0; a < swap.length; a++ )
1566 if ( swap[a] != null )
1567 stack[a].style.display = swap[a];
1570 if ( prop == "opacity" && ret == "" )
1573 } else if (elem.currentStyle) {
1574 var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});
1575 ret = elem.currentStyle[prop] || elem.currentStyle[newProp];
1581 clean: function(a, doc) {
1583 doc = doc || document;
1585 jQuery.each( a, function(i,arg){
1588 if ( arg.constructor == Number )
1589 arg = arg.toString();
1591 // Convert html string into DOM nodes
1592 if ( typeof arg == "string" ) {
1593 // Trim whitespace, otherwise indexOf won't work as expected
1594 var s = jQuery.trim(arg).toLowerCase(), div = doc.createElement("div"), tb = [];
1597 // option or optgroup
1598 !s.indexOf("<opt") &&
1599 [1, "<select>", "</select>"] ||
1601 !s.indexOf("<leg") &&
1602 [1, "<fieldset>", "</fieldset>"] ||
1604 s.match(/^<(thead|tbody|tfoot|colg|cap)/) &&
1605 [1, "<table>", "</table>"] ||
1607 !s.indexOf("<tr") &&
1608 [2, "<table><tbody>", "</tbody></table>"] ||
1610 // <thead> matched above
1611 (!s.indexOf("<td") || !s.indexOf("<th")) &&
1612 [3, "<table><tbody><tr>", "</tr></tbody></table>"] ||
1614 !s.indexOf("<col") &&
1615 [2, "<table><tbody></tbody><colgroup>", "</colgroup></table>"] ||
1617 // IE can't serialize <link> and <script> tags normally
1618 jQuery.browser.msie &&
1619 [1, "div<div>", "</div>"] ||
1623 // Go to html and back, then peel off extra wrappers
1624 div.innerHTML = wrap[1] + arg + wrap[2];
1626 // Move to the right depth
1628 div = div.lastChild;
1630 // Remove IE's autoinserted <tbody> from table fragments
1631 if ( jQuery.browser.msie ) {
1633 // String was a <table>, *may* have spurious <tbody>
1634 if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 )
1635 tb = div.firstChild && div.firstChild.childNodes;
1637 // String was a bare <thead> or <tfoot>
1638 else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 )
1639 tb = div.childNodes;
1641 for ( var n = tb.length-1; n >= 0 ; --n )
1642 if ( jQuery.nodeName(tb[n], "tbody") && !tb[n].childNodes.length )
1643 tb[n].parentNode.removeChild(tb[n]);
1645 // IE completely kills leading whitespace when innerHTML is used
1646 if ( /^\s/.test(arg) )
1647 div.insertBefore( doc.createTextNode( arg.match(/^\s*/)[0] ), div.firstChild );
1651 arg = jQuery.makeArray( div.childNodes );
1654 if ( 0 === arg.length && (!jQuery.nodeName(arg, "form") && !jQuery.nodeName(arg, "select")) )
1657 if ( arg[0] == undefined || jQuery.nodeName(arg, "form") || arg.options )
1660 r = jQuery.merge( r, arg );
1667 attr: function(elem, name, value){
1668 var fix = jQuery.isXMLDoc(elem) ? {} : jQuery.props;
1670 // Safari mis-reports the default selected property of a hidden option
1671 // Accessing the parent's selectedIndex property fixes it
1672 if ( name == "selected" && jQuery.browser.safari )
1673 elem.parentNode.selectedIndex;
1675 // Certain attributes only work when accessed via the old DOM 0 way
1677 if ( value != undefined ) elem[fix[name]] = value;
1678 return elem[fix[name]];
1679 } else if ( jQuery.browser.msie && name == "style" )
1680 return jQuery.attr( elem.style, "cssText", value );
1682 else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName(elem, "form") && (name == "action" || name == "method") )
1683 return elem.getAttributeNode(name).nodeValue;
1685 // IE elem.getAttribute passes even for style
1686 else if ( elem.tagName ) {
1688 if ( value != undefined ) elem.setAttribute( name, value );
1689 if ( jQuery.browser.msie && /href|src/.test(name) && !jQuery.isXMLDoc(elem) )
1690 return elem.getAttribute( name, 2 );
1691 return elem.getAttribute( name );
1693 // elem is actually elem.style ... set the style
1695 // IE actually uses filters for opacity
1696 if ( name == "opacity" && jQuery.browser.msie ) {
1697 if ( value != undefined ) {
1698 // IE has trouble with opacity if it does not have layout
1699 // Force it by setting the zoom level
1702 // Set the alpha filter to set the opacity
1703 elem.filter = (elem.filter || "").replace(/alpha\([^)]*\)/,"") +
1704 (parseFloat(value).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
1707 return elem.filter ?
1708 (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() : "";
1710 name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();});
1711 if ( value != undefined ) elem[name] = value;
1717 * Remove the whitespace from the beginning and end of a string.
1719 * @example $.trim(" hello, how are you? ");
1720 * @result "hello, how are you?"
1724 * @param String str The string to trim.
1728 return (t||"").replace(/^\s+|\s+$/g, "");
1731 makeArray: function( a ) {
1734 // Need to use typeof to fight Safari childNodes crashes
1735 if ( typeof a != "array" )
1736 for ( var i = 0, al = a.length; i < al; i++ )
1744 inArray: function( b, a ) {
1745 for ( var i = 0, al = a.length; i < al; i++ )
1752 * Merge two arrays together by concatenating them.
1754 * @example $.merge( [0,1,2], [2,3,4] )
1755 * @result [0,1,2,2,3,4]
1756 * @desc Merges two arrays.
1760 * @param Array first The first array to merge, the elements of second are added.
1761 * @param Array second The second array to append to the first, unaltered.
1764 merge: function(first, second) {
1765 // We have to loop this way because IE & Opera overwrite the length
1766 // expando of getElementsByTagName
1768 // Also, we need to make sure that the correct elements are being returned
1769 // (IE returns comment nodes in a '*' query)
1770 if ( jQuery.browser.msie ) {
1771 for ( var i = 0; second[i]; i++ )
1772 if ( second[i].nodeType != 8 )
1773 first.push(second[i]);
1775 for ( var i = 0; second[i]; i++ )
1776 first.push(second[i]);
1782 * Reduce an array (of jQuery objects only) to its unique elements.
1784 * @example $.unique( [x1, x2, x3, x2, x3] )
1785 * @result [x1, x2, x3]
1786 * @desc Reduces the arrays of jQuery objects to unique elements by removing the duplicates of x2 and x3
1790 * @param Array array The array to reduce to its unique jQuery objects.
1793 unique: function(first) {
1794 var r = [], num = jQuery.mergeNum++;
1797 for ( var i = 0, fl = first.length; i < fl; i++ )
1798 if ( num != first[i].mergeNum ) {
1799 first[i].mergeNum = num;
1812 * Filter items out of an array, by using a filter function.
1814 * The specified function will be passed two arguments: The
1815 * current array item and the index of the item in the array. The
1816 * function must return 'true' to keep the item in the array,
1817 * false to remove it.
1819 * @example $.grep( [0,1,2], function(i){
1826 * @param Array array The Array to find items in.
1827 * @param Function fn The function to process each item against.
1828 * @param Boolean inv Invert the selection - select the opposite of the function.
1831 grep: function(elems, fn, inv) {
1832 // If a string is passed in for the function, make a function
1833 // for it (a handy shortcut)
1834 if ( typeof fn == "string" )
1835 fn = new Function("a","i","return " + fn);
1839 // Go through the array, only saving the items
1840 // that pass the validator function
1841 for ( var i = 0, el = elems.length; i < el; i++ )
1842 if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
1843 result.push( elems[i] );
1849 * Translate all items in an array to another array of items.
1851 * The translation function that is provided to this method is
1852 * called for each item in the array and is passed one argument:
1853 * The item to be translated.
1855 * The function can then return the translated value, 'null'
1856 * (to remove the item), or an array of values - which will
1857 * be flattened into the full array.
1859 * @example $.map( [0,1,2], function(i){
1863 * @desc Maps the original array to a new one and adds 4 to each value.
1865 * @example $.map( [0,1,2], function(i){
1866 * return i > 0 ? i + 1 : null;
1869 * @desc Maps the original array to a new one and adds 1 to each
1870 * value if it is bigger then zero, otherwise it's removed-
1872 * @example $.map( [0,1,2], function(i){
1873 * return [ i, i + 1 ];
1875 * @result [0, 1, 1, 2, 2, 3]
1876 * @desc Maps the original array to a new one, each element is added
1877 * with it's original value and the value plus one.
1881 * @param Array array The Array to translate.
1882 * @param Function fn The function to process each item against.
1885 map: function(elems, fn) {
1886 // If a string is passed in for the function, make a function
1887 // for it (a handy shortcut)
1888 if ( typeof fn == "string" )
1889 fn = new Function("a","return " + fn);
1893 // Go through the array, translating each of the items to their
1894 // new value (or values).
1895 for ( var i = 0, el = elems.length; i < el; i++ ) {
1896 var val = fn(elems[i],i);
1898 if ( val !== null && val != undefined ) {
1899 if ( val.constructor != Array ) val = [val];
1900 result = result.concat( val );
1909 * Contains flags for the useragent, read from navigator.userAgent.
1910 * Available flags are: safari, opera, msie, mozilla
1912 * This property is available before the DOM is ready, therefore you can
1913 * use it to add ready events only for certain browsers.
1915 * There are situations where object detections is not reliable enough, in that
1916 * cases it makes sense to use browser detection. Simply try to avoid both!
1918 * A combination of browser and object detection yields quite reliable results.
1920 * @example $.browser.msie
1921 * @desc Returns true if the current useragent is some version of microsoft's internet explorer
1923 * @example if($.browser.safari) { $( function() { alert("this is safari!"); } ); }
1924 * @desc Alerts "this is safari!" only for safari browsers
1933 * Whether the W3C compliant box model is being used.
1940 var userAgent = navigator.userAgent.toLowerCase();
1942 // Figure out what browser is being used
1944 version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
1945 safari: /webkit/.test(userAgent),
1946 opera: /opera/.test(userAgent),
1947 msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
1948 mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
1951 var styleFloat = jQuery.browser.msie ? "styleFloat" : "cssFloat";
1954 // Check to see if the W3C box model is being used
1955 boxModel: !jQuery.browser.msie || document.compatMode == "CSS1Compat",
1957 styleFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",
1961 "class": "className",
1962 "float": styleFloat,
1963 cssFloat: styleFloat,
1964 styleFloat: styleFloat,
1965 innerHTML: "innerHTML",
1966 className: "className",
1968 disabled: "disabled",
1970 readonly: "readOnly",
1971 selected: "selected",
1972 maxlength: "maxLength"
1977 * Get a set of elements containing the unique parents of the matched
1980 * You may use an optional expression to filter the set of parent elements that will match.
1982 * @example $("p").parent()
1983 * @before <div><p>Hello</p><p>Hello</p></div>
1984 * @result [ <div><p>Hello</p><p>Hello</p></div> ]
1985 * @desc Find the parent element of each paragraph.
1987 * @example $("p").parent(".selected")
1988 * @before <div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>
1989 * @result [ <div class="selected"><p>Hello Again</p></div> ]
1990 * @desc Find the parent element of each paragraph with a class "selected".
1994 * @param String expr (optional) An expression to filter the parents with
1995 * @cat DOM/Traversing
1999 * Get a set of elements containing the unique ancestors of the matched
2000 * set of elements (except for the root element).
2002 * The matched elements can be filtered with an optional expression.
2004 * @example $("span").parents()
2005 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
2006 * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
2007 * @desc Find all parent elements of each span.
2009 * @example $("span").parents("p")
2010 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
2011 * @result [ <p><span>Hello</span></p> ]
2012 * @desc Find all parent elements of each span that is a paragraph.
2016 * @param String expr (optional) An expression to filter the ancestors with
2017 * @cat DOM/Traversing
2021 * Get a set of elements containing the unique next siblings of each of the
2022 * matched set of elements.
2024 * It only returns the very next sibling for each element, not all
2027 * You may provide an optional expression to filter the match.
2029 * @example $("p").next()
2030 * @before <p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
2031 * @result [ <p>Hello Again</p>, <div><span>And Again</span></div> ]
2032 * @desc Find the very next sibling of each paragraph.
2034 * @example $("p").next(".selected")
2035 * @before <p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>
2036 * @result [ <p class="selected">Hello Again</p> ]
2037 * @desc Find the very next sibling of each paragraph that has a class "selected".
2041 * @param String expr (optional) An expression to filter the next Elements with
2042 * @cat DOM/Traversing
2046 * Get a set of elements containing the unique previous siblings of each of the
2047 * matched set of elements.
2049 * Use an optional expression to filter the matched set.
2051 * Only the immediately previous sibling is returned, not all previous siblings.
2053 * @example $("p").prev()
2054 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
2055 * @result [ <div><span>Hello Again</span></div> ]
2056 * @desc Find the very previous sibling of each paragraph.
2058 * @example $("p").prev(".selected")
2059 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
2060 * @result [ <div><span>Hello</span></div> ]
2061 * @desc Find the very previous sibling of each paragraph that has a class "selected".
2065 * @param String expr (optional) An expression to filter the previous Elements with
2066 * @cat DOM/Traversing
2070 * Get a set of elements containing all of the unique siblings of each of the
2071 * matched set of elements.
2073 * Can be filtered with an optional expressions.
2075 * @example $("div").siblings()
2076 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
2077 * @result [ <p>Hello</p>, <p>And Again</p> ]
2078 * @desc Find all siblings of each div.
2080 * @example $("div").siblings(".selected")
2081 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
2082 * @result [ <p class="selected">Hello Again</p> ]
2083 * @desc Find all siblings with a class "selected" of each div.
2087 * @param String expr (optional) An expression to filter the sibling Elements with
2088 * @cat DOM/Traversing
2092 * Get a set of elements containing all of the unique children of each of the
2093 * matched set of elements.
2095 * This set can be filtered with an optional expression that will cause
2096 * only elements matching the selector to be collected.
2098 * @example $("div").children()
2099 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
2100 * @result [ <span>Hello Again</span> ]
2101 * @desc Find all children of each div.
2103 * @example $("div").children(".selected")
2104 * @before <div><span>Hello</span><p class="selected">Hello Again</p><p>And Again</p></div>
2105 * @result [ <p class="selected">Hello Again</p> ]
2106 * @desc Find all children with a class "selected" of each div.
2110 * @param String expr (optional) An expression to filter the child Elements with
2111 * @cat DOM/Traversing
2114 parent: "a.parentNode",
2115 parents: "jQuery.parents(a)",
2116 next: "jQuery.nth(a,2,'nextSibling')",
2117 prev: "jQuery.nth(a,2,'previousSibling')",
2118 siblings: "jQuery.sibling(a.parentNode.firstChild,a)",
2119 children: "jQuery.sibling(a.firstChild)"
2121 jQuery.fn[ i ] = function(a) {
2122 var ret = jQuery.map(this,n);
2123 if ( a && typeof a == "string" )
2124 ret = jQuery.multiFilter(a,ret);
2125 return this.pushStack( jQuery.unique(ret) );
2130 * Append all of the matched elements to another, specified, set of elements.
2131 * This operation is, essentially, the reverse of doing a regular
2132 * $(A).append(B), in that instead of appending B to A, you're appending
2135 * @example $("p").appendTo("#foo");
2136 * @before <p>I would like to say: </p><div id="foo"></div>
2137 * @result <div id="foo"><p>I would like to say: </p></div>
2138 * @desc Appends all paragraphs to the element with the ID "foo"
2142 * @param <Content> content Content to append to the selected element to.
2143 * @cat DOM/Manipulation
2144 * @see append(<Content>)
2148 * Prepend all of the matched elements to another, specified, set of elements.
2149 * This operation is, essentially, the reverse of doing a regular
2150 * $(A).prepend(B), in that instead of prepending B to A, you're prepending
2153 * @example $("p").prependTo("#foo");
2154 * @before <p>I would like to say: </p><div id="foo"><b>Hello</b></div>
2155 * @result <div id="foo"><p>I would like to say: </p><b>Hello</b></div>
2156 * @desc Prepends all paragraphs to the element with the ID "foo"
2160 * @param <Content> content Content to prepend to the selected element to.
2161 * @cat DOM/Manipulation
2162 * @see prepend(<Content>)
2166 * Insert all of the matched elements before another, specified, set of elements.
2167 * This operation is, essentially, the reverse of doing a regular
2168 * $(A).before(B), in that instead of inserting B before A, you're inserting
2171 * @example $("p").insertBefore("#foo");
2172 * @before <div id="foo">Hello</div><p>I would like to say: </p>
2173 * @result <p>I would like to say: </p><div id="foo">Hello</div>
2174 * @desc Same as $("#foo").before("p")
2176 * @name insertBefore
2178 * @param <Content> content Content to insert the selected element before.
2179 * @cat DOM/Manipulation
2180 * @see before(<Content>)
2184 * Insert all of the matched elements after another, specified, set of elements.
2185 * This operation is, essentially, the reverse of doing a regular
2186 * $(A).after(B), in that instead of inserting B after A, you're inserting
2189 * @example $("p").insertAfter("#foo");
2190 * @before <p>I would like to say: </p><div id="foo">Hello</div>
2191 * @result <div id="foo">Hello</div><p>I would like to say: </p>
2192 * @desc Same as $("#foo").after("p")
2196 * @param <Content> content Content to insert the selected element after.
2197 * @cat DOM/Manipulation
2198 * @see after(<Content>)
2203 prependTo: "prepend",
2204 insertBefore: "before",
2205 insertAfter: "after"
2207 jQuery.fn[ i ] = function(){
2209 return this.each(function(){
2210 for ( var j = 0, al = a.length; j < al; j++ )
2211 jQuery(a[j])[n]( this );
2217 * Remove an attribute from each of the matched elements.
2219 * @example $("input").removeAttr("disabled")
2220 * @before <input disabled="disabled"/>
2225 * @param String name The name of the attribute to remove.
2226 * @cat DOM/Attributes
2230 * Adds the specified class(es) to each of the set of matched elements.
2232 * @example $("p").addClass("selected")
2233 * @before <p>Hello</p>
2234 * @result [ <p class="selected">Hello</p> ]
2236 * @example $("p").addClass("selected highlight")
2237 * @before <p>Hello</p>
2238 * @result [ <p class="selected highlight">Hello</p> ]
2242 * @param String class One or more CSS classes to add to the elements
2243 * @cat DOM/Attributes
2244 * @see removeClass(String)
2248 * Removes all or the specified class(es) from the set of matched elements.
2250 * @example $("p").removeClass()
2251 * @before <p class="selected">Hello</p>
2252 * @result [ <p>Hello</p> ]
2254 * @example $("p").removeClass("selected")
2255 * @before <p class="selected first">Hello</p>
2256 * @result [ <p class="first">Hello</p> ]
2258 * @example $("p").removeClass("selected highlight")
2259 * @before <p class="highlight selected first">Hello</p>
2260 * @result [ <p class="first">Hello</p> ]
2264 * @param String class (optional) One or more CSS classes to remove from the elements
2265 * @cat DOM/Attributes
2266 * @see addClass(String)
2270 * Adds the specified class if it is not present, removes it if it is
2273 * @example $("p").toggleClass("selected")
2274 * @before <p>Hello</p><p class="selected">Hello Again</p>
2275 * @result [ <p class="selected">Hello</p>, <p>Hello Again</p> ]
2279 * @param String class A CSS class with which to toggle the elements
2280 * @cat DOM/Attributes
2284 * Removes all matched elements from the DOM. This does NOT remove them from the
2285 * jQuery object, allowing you to use the matched elements further.
2287 * Can be filtered with an optional expressions.
2289 * @example $("p").remove();
2290 * @before <p>Hello</p> how are <p>you?</p>
2293 * @example $("p").remove(".hello");
2294 * @before <p class="hello">Hello</p> how are <p>you?</p>
2295 * @result how are <p>you?</p>
2299 * @param String expr (optional) A jQuery expression to filter elements by.
2300 * @cat DOM/Manipulation
2304 * Removes all child nodes from the set of matched elements.
2306 * @example $("p").empty()
2307 * @before <p>Hello, <span>Person</span> <a href="#">and person</a></p>
2308 * @result [ <p></p> ]
2312 * @cat DOM/Manipulation
2316 removeAttr: function( key ) {
2317 jQuery.attr( this, key, "" );
2318 this.removeAttribute( key );
2320 addClass: function(c){
2321 jQuery.className.add(this,c);
2323 removeClass: function(c){
2324 jQuery.className.remove(this,c);
2326 toggleClass: function( c ){
2327 jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);
2329 remove: function(a){
2330 if ( !a || jQuery.filter( a, [this] ).r.length )
2331 this.parentNode.removeChild( this );
2334 while ( this.firstChild )
2335 this.removeChild( this.firstChild );
2338 jQuery.fn[ i ] = function() {
2339 return this.each( n, arguments );
2344 * Reduce the set of matched elements to a single element.
2345 * The position of the element in the set of matched elements
2346 * starts at 0 and goes to length - 1.
2348 * @example $("p").eq(1)
2349 * @before <p>This is just a test.</p><p>So is this</p>
2350 * @result [ <p>So is this</p> ]
2354 * @param Number pos The index of the element that you wish to limit to.
2359 * Reduce the set of matched elements to all elements before a given position.
2360 * The position of the element in the set of matched elements
2361 * starts at 0 and goes to length - 1.
2363 * @example $("p").lt(1)
2364 * @before <p>This is just a test.</p><p>So is this</p>
2365 * @result [ <p>This is just a test.</p> ]
2369 * @param Number pos Reduce the set to all elements below this position.
2374 * Reduce the set of matched elements to all elements after a given position.
2375 * The position of the element in the set of matched elements
2376 * starts at 0 and goes to length - 1.
2378 * @example $("p").gt(0)
2379 * @before <p>This is just a test.</p><p>So is this</p>
2380 * @result [ <p>So is this</p> ]
2384 * @param Number pos Reduce the set to all elements after this position.
2389 * Filter the set of elements to those that contain the specified text.
2391 * @example $("p").contains("test")
2392 * @before <p>This is just a test.</p><p>So is this</p>
2393 * @result [ <p>This is just a test.</p> ]
2397 * @param String str The string that will be contained within the text of an element.
2398 * @cat DOM/Traversing
2400 jQuery.each( [ "eq", "lt", "gt", "contains" ], function(i,n){
2401 jQuery.fn[ n ] = function(num,fn) {
2402 return this.filter( ":" + n + "(" + num + ")", fn );
2407 * Get the current computed, pixel, width of the first matched element.
2409 * @example $("p").width();
2410 * @before <p>This is just a test.</p>
2419 * Set the CSS width of every matched element. If no explicit unit
2420 * was specified (like 'em' or '%') then "px" is added to the width.
2422 * @example $("p").width(20);
2423 * @before <p>This is just a test.</p>
2424 * @result <p style="width:20px;">This is just a test.</p>
2426 * @example $("p").width("20em");
2427 * @before <p>This is just a test.</p>
2428 * @result <p style="width:20em;">This is just a test.</p>
2432 * @param String|Number val Set the CSS property to the specified value.
2437 * Get the current computed, pixel, height of the first matched element.
2439 * @example $("p").height();
2440 * @before <p>This is just a test.</p>
2449 * Set the CSS height of every matched element. If no explicit unit
2450 * was specified (like 'em' or '%') then "px" is added to the width.
2452 * @example $("p").height(20);
2453 * @before <p>This is just a test.</p>
2454 * @result <p style="height:20px;">This is just a test.</p>
2456 * @example $("p").height("20em");
2457 * @before <p>This is just a test.</p>
2458 * @result <p style="height:20em;">This is just a test.</p>
2462 * @param String|Number val Set the CSS property to the specified value.
2466 jQuery.each( [ "height", "width" ], function(i,n){
2467 jQuery.fn[ n ] = function(h) {
2468 return h == undefined ?
2469 ( this.length ? jQuery.css( this[0], n ) : null ) :
2470 this.css( n, h.constructor == String ? h : h + "px" );