2 * jQuery @VERSION - New Wave Javascript
4 * Copyright (c) 2006 John Resig (jquery.com)
5 * Dual licensed under the MIT (MIT-LICENSE.txt)
6 * and GPL (GPL-LICENSE.txt) licenses.
12 // Global undefined variable
13 window.undefined = window.undefined;
16 * Create a new jQuery Object
21 * @param String|Function|Element|Array<Element>|jQuery a selector
22 * @param jQuery|Element|Array<Element> c context
25 var jQuery = function(a,c) {
26 // If the context is global, return a new object
28 return new jQuery(a,c);
30 // Make sure that a selection was provided
33 // HANDLE: $(function)
34 // Shortcut for document ready
35 // Safari reports typeof on DOM NodeLists as a function
36 if ( typeof a == "function" && !a.nodeType && a[0] == undefined )
37 return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a );
39 // Handle HTML strings
40 if ( typeof a == "string" ) {
41 // HANDLE: $(html) -> $(array)
42 var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
44 a = jQuery.clean( [ m[1] ] );
48 return new jQuery( c ).find( a );
53 a.constructor == Array && a ||
55 // HANDLE: $(arraylike)
56 // Watch for when an array-like object is passed as the selector
57 (a.jquery || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType) && jQuery.makeArray( a ) ||
63 // Map over the $ in case of overwrite
64 if ( typeof $ != "undefined" )
67 // Map the jQuery namespace to the '$' one
71 * This function accepts a string containing a CSS or
72 * basic XPath selector which is then used to match a set of elements.
74 * The core functionality of jQuery centers around this function.
75 * Everything in jQuery is based upon this, or uses this in some way.
76 * The most basic use of this function is to pass in an expression
77 * (usually consisting of CSS or XPath), which then finds all matching
80 * By default, $() looks for DOM elements within the context of the
81 * current HTML document.
83 * @example $("div > p")
84 * @desc Finds all p elements that are children of a div element.
85 * @before <p>one</p> <div><p>two</p></div> <p>three</p>
86 * @result [ <p>two</p> ]
88 * @example $("input:radio", document.forms[0])
89 * @desc Searches for all inputs of type radio within the first form in the document
91 * @example $("div", xml.responseXML)
92 * @desc This finds all div elements within the specified XML document.
95 * @param String expr An expression to search with
96 * @param Element|jQuery context (optional) A DOM Element, Document or jQuery to use as context
100 * @see $(Element<Array>)
104 * Create DOM elements on-the-fly from the provided String of raw HTML.
106 * @example $("<div><p>Hello</p></div>").appendTo("#body")
107 * @desc Creates a div element (and all of its contents) dynamically,
108 * and appends it to the element with the ID of body. Internally, an
109 * element is created and it's innerHTML property set to the given markup.
110 * It is therefore both quite flexible and limited.
113 * @param String html A string of HTML to create on the fly.
116 * @see appendTo(String)
120 * Wrap jQuery functionality around a single or multiple DOM Element(s).
122 * This function also accepts XML Documents and Window objects
123 * as valid arguments (even though they are not DOM Elements).
125 * @example $(document).find("div > p")
126 * @before <p>one</p> <div><p>two</p></div> <p>three</p>
127 * @result [ <p>two</p> ]
128 * @desc Same as $("div > p") because the document
130 * @example $(document.body).background( "black" );
131 * @desc Sets the background color of the page to black.
133 * @example $( myForm.elements ).hide()
134 * @desc Hides all the input elements within a form
137 * @param Element|Array<Element> elems DOM element(s) to be encapsulated by a jQuery object.
143 * A shorthand for $(document).ready(), allowing you to bind a function
144 * to be executed when the DOM document has finished loading. This function
145 * behaves just like $(document).ready(), in that it should be used to wrap
146 * all of the other $() operations on your page. While this function is,
147 * technically, chainable - there really isn't much use for chaining against it.
148 * You can have as many $(document).ready events on your page as you like.
150 * See ready(Function) for details about the ready event.
152 * @example $(function(){
153 * // Document is ready
155 * @desc Executes the function when the DOM is ready to be used.
158 * @param Function fn The function to execute when the DOM is ready.
164 * A means of creating a cloned copy of a jQuery object. This function
165 * copies the set of matched elements from one jQuery object and creates
166 * another, new, jQuery object containing the same elements.
168 * @example var div = $("div");
169 * $( div ).find("p");
170 * @desc Locates all p elements with all div elements, without disrupting the original jQuery object contained in 'div' (as would normally be the case if a simple div.find("p") was done).
173 * @param jQuery obj The jQuery object to be cloned.
178 jQuery.fn = jQuery.prototype = {
180 * The current version of jQuery.
191 * The number of elements currently matched.
193 * @example $("img").length;
194 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
204 * The number of elements currently matched.
206 * @example $("img").size();
207 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
221 * Access all matched elements. This serves as a backwards-compatible
222 * way of accessing all matched elements (other than the jQuery object
223 * itself, which is, in fact, an array of elements).
225 * @example $("img").get();
226 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
227 * @result [ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
228 * @desc Selects all images in the document and returns the DOM Elements as an Array
231 * @type Array<Element>
236 * Access a single matched element. num is used to access the
237 * Nth element matched.
239 * @example $("img").get(0);
240 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
241 * @result [ <img src="test1.jpg"/> ]
242 * @desc Selects all images in the document and returns the first one
246 * @param Number num Access the element in the Nth position.
249 get: function( num ) {
250 return num == undefined ?
252 // Return a 'clean' array
253 jQuery.makeArray( this ) :
255 // Return just the object
260 * Set the jQuery object to an array of elements, while maintaining
263 * @example $("img").set([ document.body ]);
264 * @result $("img").set() == [ document.body ]
269 * @param Elements elems An array of elements
273 var ret = jQuery(this);
274 ret.prevObject = this;
275 return ret.setArray( a );
279 * Set the jQuery object to an array of elements. This operation is
280 * completely destructive - be sure to use .set() if you wish to maintain
283 * @example $("img").setArray([ document.body ]);
284 * @result $("img").setArray() == [ document.body ]
289 * @param Elements elems An array of elements
292 setArray: function( a ) {
294 [].push.apply( this, a );
299 * Execute a function within the context of every matched element.
300 * This means that every time the passed-in function is executed
301 * (which is once for every element matched) the 'this' keyword
302 * points to the specific element.
304 * Additionally, the function, when executed, is passed a single
305 * argument representing the position of the element in the matched
308 * @example $("img").each(function(i){
309 * this.src = "test" + i + ".jpg";
311 * @before <img/><img/>
312 * @result <img src="test0.jpg"/><img src="test1.jpg"/>
313 * @desc Iterates over two images and sets their src property
317 * @param Function fn A function to execute
320 each: function( fn, args ) {
321 return jQuery.each( this, fn, args );
325 * Searches every matched element for the object and returns
326 * the index of the element, if found, starting with zero.
327 * Returns -1 if the object wasn't found.
329 * @example $("*").index( $('#foobar')[0] )
330 * @before <div id="foobar"></div><b></b><span id="foo"></span>
332 * @desc Returns the index for the element with ID foobar
334 * @example $("*").index( $('#foo'))
335 * @before <div id="foobar"></div><b></b><span id="foo"></span>
337 * @desc Returns the index for the element with ID foo
339 * @example $("*").index( $('#bar'))
340 * @before <div id="foobar"></div><b></b><span id="foo"></span>
342 * @desc Returns -1, as there is no element with ID bar
346 * @param Element subject Object to search for
349 index: function( obj ) {
351 this.each(function(i){
352 if ( this == obj ) pos = i;
358 * Access a property on the first matched element.
359 * This method makes it easy to retrieve a property value
360 * from the first matched element.
362 * @example $("img").attr("src");
363 * @before <img src="test.jpg"/>
365 * @desc Returns the src attribute from the first image in the document.
369 * @param String name The name of the property to access.
370 * @cat DOM/Attributes
374 * Set a key/value object as properties to all matched elements.
376 * This serves as the best way to set a large number of properties
377 * on all matched elements.
379 * @example $("img").attr({ src: "test.jpg", alt: "Test Image" });
381 * @result <img src="test.jpg" alt="Test Image"/>
382 * @desc Sets src and alt attributes to all images.
386 * @param Map properties Key/value pairs to set as object properties.
387 * @cat DOM/Attributes
391 * Set a single property to a value, on all matched elements.
393 * Can compute values provided as ${formula}, see second example.
395 * Note that you can't set the name property of input elements in IE.
396 * Use $(html) or .append(html) or .html(html) to create elements
397 * on the fly including the name property.
399 * @example $("img").attr("src","test.jpg");
401 * @result <img src="test.jpg"/>
402 * @desc Sets src attribute to all images.
404 * @example $("img").attr("title", "${this.src}");
405 * @before <img src="test.jpg" />
406 * @result <img src="test.jpg" title="test.jpg" />
407 * @desc Sets title attribute from src attribute, a shortcut for attr(String,Function)
411 * @param String key The name of the property to set.
412 * @param Object value The value to set the property to.
413 * @cat DOM/Attributes
417 * Set a single property to a computed value, on all matched elements.
419 * Instead of a value, a function is provided, that computes the value.
421 * @example $("img").attr("title", function() { return this.src });
422 * @before <img src="test.jpg" />
423 * @result <img src="test.jpg" title="test.jpg" />
424 * @desc Sets title attribute from src attribute.
428 * @param String key The name of the property to set.
429 * @param Function value A function returning the value to set.
430 * @cat DOM/Attributes
432 attr: function( key, value, type ) {
433 // Check to see if we're setting style values
434 return typeof key != "string" || value != undefined ?
435 this.each(function(){
436 // See if we're setting a hash of styles
437 if ( value == undefined )
438 // Set all the styles
439 for ( var prop in key )
441 type ? this.style : this,
442 prop, jQuery.parseSetter(key[prop])
445 // See if we're setting a single key/value style
447 // convert ${this.property} to function returnung that property
449 type ? this.style : this,
450 key, jQuery.parseSetter(value)
455 // Look for the case where we're accessing a style value
456 jQuery[ type || "attr" ]( this[0], key );
460 * Access a style property on the first matched element.
461 * This method makes it easy to retrieve a style property value
462 * from the first matched element.
464 * @example $("p").css("color");
465 * @before <p style="color:red;">Test Paragraph.</p>
467 * @desc Retrieves the color style of the first paragraph
469 * @example $("p").css("font-weight");
470 * @before <p style="font-weight: bold;">Test Paragraph.</p>
472 * @desc Retrieves the font-weight style of the first paragraph.
476 * @param String name The name of the property to access.
481 * Set a key/value object as style properties to all matched elements.
483 * This serves as the best way to set a large number of style properties
484 * on all matched elements.
486 * @example $("p").css({ color: "red", background: "blue" });
487 * @before <p>Test Paragraph.</p>
488 * @result <p style="color:red; background:blue;">Test Paragraph.</p>
489 * @desc Sets color and background styles to all p elements.
493 * @param Map properties Key/value pairs to set as style properties.
498 * Set a single style property to a value, on all matched elements.
500 * @example $("p").css("color","red");
501 * @before <p>Test Paragraph.</p>
502 * @result <p style="color:red;">Test Paragraph.</p>
503 * @desc Changes the color of all paragraphs to red
507 * @param String key The name of the property to set.
508 * @param Object value The value to set the property to.
511 css: function( key, value ) {
512 return this.attr( key, value, "curCSS" );
516 * Get the text contents of all matched elements. The result is
517 * a string that contains the combined text contents of all matched
518 * elements. This method works on both HTML and XML documents.
520 * @example $("p").text();
521 * @before <p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>
522 * @result Test Paragraph.Paraparagraph
523 * @desc Gets the concatenated text of all paragraphs
527 * @cat DOM/Attributes
531 * Set the text contents of all matched elements.
533 * Similar to html(), but escapes HTML (replace "<" and ">" with their
536 * If stripTags argument is set to true, HTML is stripped.
538 * @example $("p").text("<b>Some</b> new text.");
539 * @before <p>Test Paragraph.</p>
540 * @result <p><b>Some</b> new text.</p>
541 * @desc Sets the text of all paragraphs.
543 * @example $("p").text("<b>Some</b> new text.", true);
544 * @before <p>Test Paragraph.</p>
545 * @result <p>Some new text.</p>
546 * @desc Sets the text of all paragraphs.
550 * @param String val The text value to set the contents of the element to.
551 * @param Boolean stripTags (optional) Wheather to strip or only escape tags
552 * @cat DOM/Attributes
554 text: function(e, stripTags) {
555 if ( typeof e == "string" )
556 return this.html( stripTags ? e.replace(/<\/?[^>]+>/gi, '') : e.replace(/</g, "<").replace(/>/g, ">") );
560 for ( var j = 0, el = e.length; j < el; j++ ) {
561 var r = e[j].childNodes;
562 for ( var i = 0, rl = r.length; i < rl; i++ )
563 if ( r[i].nodeType != 8 )
564 t += r[i].nodeType != 1 ?
565 r[i].nodeValue : jQuery.fn.text([ r[i] ]);
571 * Wrap all matched elements with a structure of other elements.
572 * This wrapping process is most useful for injecting additional
573 * stucture into a document, without ruining the original semantic
574 * qualities of a document.
576 * This works by going through the first element
577 * provided (which is generated, on the fly, from the provided HTML)
578 * and finds the deepest ancestor element within its
579 * structure - it is that element that will en-wrap everything else.
581 * This does not work with elements that contain text. Any necessary text
582 * must be added after the wrapping is done.
584 * @example $("p").wrap("<div class='wrap'></div>");
585 * @before <p>Test Paragraph.</p>
586 * @result <div class='wrap'><p>Test Paragraph.</p></div>
590 * @param String html A string of HTML, that will be created on the fly and wrapped around the target.
591 * @cat DOM/Manipulation
595 * Wrap all matched elements with a structure of other elements.
596 * This wrapping process is most useful for injecting additional
597 * stucture into a document, without ruining the original semantic
598 * qualities of a document.
600 * This works by going through the first element
601 * provided and finding the deepest ancestor element within its
602 * structure - it is that element that will en-wrap everything else.
604 * This does not work with elements that contain text. Any necessary text
605 * must be added after the wrapping is done.
607 * @example $("p").wrap( document.getElementById('content') );
608 * @before <p>Test Paragraph.</p><div id="content"></div>
609 * @result <div id="content"><p>Test Paragraph.</p></div>
613 * @param Element elem A DOM element that will be wrapped around the target.
614 * @cat DOM/Manipulation
617 // The elements to wrap the target around
618 var a = jQuery.clean(arguments);
620 // Wrap each of the matched elements individually
621 return this.each(function(){
622 // Clone the structure that we're using to wrap
623 var b = a[0].cloneNode(true);
625 // Insert it before the element to be wrapped
626 this.parentNode.insertBefore( b, this );
628 // Find the deepest point in the wrap structure
629 while ( b.firstChild )
632 // Move the matched element to within the wrap structure
633 b.appendChild( this );
638 * Append content to the inside of every matched element.
640 * This operation is similar to doing an appendChild to all the
641 * specified elements, adding them into the document.
643 * @example $("p").append("<b>Hello</b>");
644 * @before <p>I would like to say: </p>
645 * @result <p>I would like to say: <b>Hello</b></p>
646 * @desc Appends some HTML to all paragraphs.
648 * @example $("p").append( $("#foo")[0] );
649 * @before <p>I would like to say: </p><b id="foo">Hello</b>
650 * @result <p>I would like to say: <b id="foo">Hello</b></p>
651 * @desc Appends an Element to all paragraphs.
653 * @example $("p").append( $("b") );
654 * @before <p>I would like to say: </p><b>Hello</b>
655 * @result <p>I would like to say: <b>Hello</b></p>
656 * @desc Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
660 * @param <Content> content Content to append to the target
661 * @cat DOM/Manipulation
662 * @see prepend(<Content>)
663 * @see before(<Content>)
664 * @see after(<Content>)
667 return this.domManip(arguments, true, 1, function(a){
668 this.appendChild( a );
673 * Prepend content to the inside of every matched element.
675 * This operation is the best way to insert elements
676 * inside, at the beginning, of all matched elements.
678 * @example $("p").prepend("<b>Hello</b>");
679 * @before <p>I would like to say: </p>
680 * @result <p><b>Hello</b>I would like to say: </p>
681 * @desc Prepends some HTML to all paragraphs.
683 * @example $("p").prepend( $("#foo")[0] );
684 * @before <p>I would like to say: </p><b id="foo">Hello</b>
685 * @result <p><b id="foo">Hello</b>I would like to say: </p>
686 * @desc Prepends an Element to all paragraphs.
688 * @example $("p").prepend( $("b") );
689 * @before <p>I would like to say: </p><b>Hello</b>
690 * @result <p><b>Hello</b>I would like to say: </p>
691 * @desc Prepends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.
695 * @param <Content> content Content to prepend to the target.
696 * @cat DOM/Manipulation
697 * @see append(<Content>)
698 * @see before(<Content>)
699 * @see after(<Content>)
701 prepend: function() {
702 return this.domManip(arguments, true, -1, function(a){
703 this.insertBefore( a, this.firstChild );
708 * Insert content before each of the matched elements.
710 * @example $("p").before("<b>Hello</b>");
711 * @before <p>I would like to say: </p>
712 * @result <b>Hello</b><p>I would like to say: </p>
713 * @desc Inserts some HTML before all paragraphs.
715 * @example $("p").before( $("#foo")[0] );
716 * @before <p>I would like to say: </p><b id="foo">Hello</b>
717 * @result <b id="foo">Hello</b><p>I would like to say: </p>
718 * @desc Inserts an Element before all paragraphs.
720 * @example $("p").before( $("b") );
721 * @before <p>I would like to say: </p><b>Hello</b>
722 * @result <b>Hello</b><p>I would like to say: </p>
723 * @desc Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs.
727 * @param <Content> content Content to insert before each target.
728 * @cat DOM/Manipulation
729 * @see append(<Content>)
730 * @see prepend(<Content>)
731 * @see after(<Content>)
734 return this.domManip(arguments, false, 1, function(a){
735 this.parentNode.insertBefore( a, this );
740 * Insert content after each of the matched elements.
742 * @example $("p").after("<b>Hello</b>");
743 * @before <p>I would like to say: </p>
744 * @result <p>I would like to say: </p><b>Hello</b>
745 * @desc Inserts some HTML after all paragraphs.
747 * @example $("p").after( $("#foo")[0] );
748 * @before <b id="foo">Hello</b><p>I would like to say: </p>
749 * @result <p>I would like to say: </p><b id="foo">Hello</b>
750 * @desc Inserts an Element after all paragraphs.
752 * @example $("p").after( $("b") );
753 * @before <b>Hello</b><p>I would like to say: </p>
754 * @result <p>I would like to say: </p><b>Hello</b>
755 * @desc Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs.
759 * @param <Content> content Content to insert after each target.
760 * @cat DOM/Manipulation
761 * @see append(<Content>)
762 * @see prepend(<Content>)
763 * @see before(<Content>)
766 return this.domManip(arguments, false, -1, function(a){
767 this.parentNode.insertBefore( a, this.nextSibling );
772 * End the most recent 'destructive' operation, reverting the list of matched elements
773 * back to its previous state. After an end operation, the list of matched elements will
774 * revert to the last state of matched elements.
776 * If there was no destructive operation before, an empty set is returned.
778 * @example $("p").find("span").end();
779 * @before <p><span>Hello</span>, how are you?</p>
780 * @result [ <p>...</p> ]
781 * @desc Selects all paragraphs, finds span elements inside these, and reverts the
782 * selection back to the paragraphs.
786 * @cat DOM/Traversing
789 return this.prevObject || jQuery([]);
793 * Searches for all elements that match the specified expression.
795 * This method is a good way to find additional descendant
796 * elements with which to process.
798 * All searching is done using a jQuery expression. The expression can be
799 * written using CSS 1-3 Selector syntax, or basic XPath.
801 * @example $("p").find("span");
802 * @before <p><span>Hello</span>, how are you?</p>
803 * @result [ <span>Hello</span> ]
804 * @desc Starts with all paragraphs and searches for descendant span
805 * elements, same as $("p span")
809 * @param String expr An expression to search with.
810 * @cat DOM/Traversing
813 return this.set( jQuery.map( this, function(a){
814 return jQuery.find(t,a);
819 * Clone matched DOM Elements and select the clones.
821 * This is useful for moving copies of the elements to another
822 * location in the DOM.
824 * @example $("b").clone().prependTo("p");
825 * @before <b>Hello</b><p>, how are you?</p>
826 * @result <b>Hello</b><p><b>Hello</b>, how are you?</p>
827 * @desc Clones all b elements (and selects the clones) and prepends them to all paragraphs.
831 * @cat DOM/Manipulation
833 clone: function(deep) {
834 return this.set( jQuery.map( this, function(a){
835 return a.cloneNode( deep != undefined ? deep : true );
840 * Removes all elements from the set of matched elements that do not
841 * match the specified expression(s). This method is used to narrow down
842 * the results of a search.
844 * Provide a String array of expressions to apply multiple filters at once.
846 * @example $("p").filter(".selected")
847 * @before <p class="selected">Hello</p><p>How are you?</p>
848 * @result [ <p class="selected">Hello</p> ]
849 * @desc Selects all paragraphs and removes those without a class "selected".
851 * @example $("p").filter([".selected", ":first"])
852 * @before <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
853 * @result [ <p>Hello</p>, <p class="selected">And Again</p> ]
854 * @desc Selects all paragraphs and removes those without class "selected" and being the first one.
858 * @param String|Array<String> expression Expression(s) to search with.
859 * @cat DOM/Traversing
863 * Removes all elements from the set of matched elements that do not
864 * pass the specified filter. This method is used to narrow down
865 * the results of a search.
867 * @example $("p").filter(function(index) {
868 * return $("ol", this).length == 0;
870 * @before <p><ol><li>Hello</li></ol></p><p>How are you?</p>
871 * @result [ <p>How are you?</p> ]
872 * @desc Remove all elements that have a child ol element
876 * @param Function filter A function to use for filtering
877 * @cat DOM/Traversing
879 filter: function(t) {
881 t.constructor == Array &&
882 jQuery.map(this,function(a){
883 for ( var i = 0, tl = t.length; i < tl; i++ )
884 if ( jQuery.filter(t[i],[a]).r.length )
889 t.constructor == Boolean &&
890 ( t ? this.get() : [] ) ||
892 typeof t == "function" &&
893 jQuery.grep( this, function(el, index) { return t.apply(el, [index]) }) ||
895 jQuery.filter(t,this).r );
899 * Removes the specified Element from the set of matched elements. This
900 * method is used to remove a single Element from a jQuery object.
902 * @example $("p").not( $("#selected")[0] )
903 * @before <p>Hello</p><p id="selected">Hello Again</p>
904 * @result [ <p>Hello</p> ]
905 * @desc Removes the element with the ID "selected" from the set of all paragraphs.
909 * @param Element el An element to remove from the set
910 * @cat DOM/Traversing
914 * Removes elements matching the specified expression from the set
915 * of matched elements. This method is used to remove one or more
916 * elements from a jQuery object.
918 * @example $("p").not("#selected")
919 * @before <p>Hello</p><p id="selected">Hello Again</p>
920 * @result [ <p>Hello</p> ]
921 * @desc Removes the element with the ID "selected" from the set of all paragraphs.
925 * @param String expr An expression with which to remove matching elements
926 * @cat DOM/Traversing
929 return this.set( typeof t == "string" ?
930 jQuery.filter(t,this,true).r :
931 jQuery.grep(this,function(a){ return a != t; }) );
935 * Adds the elements matched by the expression to the jQuery object. This
936 * can be used to concatenate the result sets of two expressions.
938 * @example $("p").add("span")
939 * @before <p>Hello</p><p><span>Hello Again</span></p>
940 * @result [ <p>Hello</p>, <span>Hello Again</span> ]
944 * @param String expr An expression whose matched elements are added
945 * @cat DOM/Traversing
949 * Adds one or more Elements to the set of matched elements.
951 * This is used to add a set of Elements to a jQuery object.
953 * @example $("p").add( document.getElementById("a") )
954 * @before <p>Hello</p><p><span id="a">Hello Again</span></p>
955 * @result [ <p>Hello</p>, <span id="a">Hello Again</span> ]
957 * @example $("p").add([document.getElementById("a"), document.getElementById("b")])
958 * @before <p>Hello</p><p><span id="a">Hello Again</span><span id="b">And Again</span></p>
959 * @result [ <p>Hello</p>, <span id="a">Hello Again</span>, <span id="b">And Again</span> ]
963 * @param Element|Array<Element> elements One or more Elements to add
964 * @cat DOM/Traversing
967 return this.set( jQuery.merge(
968 this.get(), typeof t == "string" ?
970 t.constructor == Array ? t : [t] ) );
974 * Checks the current selection against an expression and returns true,
975 * if at least one element of the selection fits the given expression.
977 * Does return false, if no element fits or the expression is not valid.
979 * filter(String) is used internally, therefore all rules that apply there
982 * @example $("input[@type='checkbox']").parent().is("form")
983 * @before <form><input type="checkbox" /></form>
985 * @desc Returns true, because the parent of the input is a form element
987 * @example $("input[@type='checkbox']").parent().is("form")
988 * @before <form><p><input type="checkbox" /></p></form>
990 * @desc Returns false, because the parent of the input is a p element
994 * @param String expr The expression with which to filter
995 * @cat DOM/Traversing
998 return expr ? jQuery.filter(expr,this).r.length > 0 : false;
1002 * Get the current value of the first matched element.
1004 * @example $("input").val();
1005 * @before <input type="text" value="some text"/>
1006 * @result "some text"
1010 * @cat DOM/Attributes
1014 * Set the value of every matched element.
1016 * @example $("input").val("test");
1017 * @before <input type="text" value="some text"/>
1018 * @result <input type="text" value="test"/>
1022 * @param String val Set the property to the specified value.
1023 * @cat DOM/Attributes
1025 val: function( val ) {
1026 return val == undefined ?
\r ( this.length ? this[0].value : null ) :
\r this.attr( "value", val );
1030 * Get the html contents of the first matched element.
1031 * This property is not available on XML documents.
1033 * @example $("div").html();
1034 * @before <div><input/></div>
1039 * @cat DOM/Attributes
1043 * Set the html contents of every matched element.
1044 * This property is not available on XML documents.
1046 * @example $("div").html("<b>new stuff</b>");
1047 * @before <div><input/></div>
1048 * @result <div><b>new stuff</b></div>
1052 * @param String val Set the html contents to the specified value.
1053 * @cat DOM/Attributes
1055 html: function( val ) {
1056 return val == undefined ?
\r ( this.length ? this[0].innerHTML : null ) :
\r this.attr( "innerHTML", val );
1063 * @param Boolean table Insert TBODY in TABLEs if one is not found.
1064 * @param Number dir If dir<0, process args in reverse order.
1065 * @param Function fn The function doing the DOM manipulation.
1069 domManip: function(args, table, dir, fn){
1070 var clone = this.length > 1;
1071 var a = jQuery.clean(args);
1075 return this.each(function(){
1078 if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" )
1079 obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));
1081 for ( var i = 0, al = a.length; i < al; i++ )
1082 fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );
1089 * Extends the jQuery object itself. Can be used to add functions into
1090 * the jQuery namespace and to add plugin methods (plugins).
1092 * @example jQuery.fn.extend({
1093 * check: function() {
1094 * return this.each(function() { this.checked = true; });
1096 * uncheck: function() {
1097 * return this.each(function() { this.checked = false; });
1100 * $("input[@type=checkbox]").check();
1101 * $("input[@type=radio]").uncheck();
1102 * @desc Adds two plugin methods.
1104 * @example jQuery.extend({
1105 * min: function(a, b) { return a < b ? a : b; },
1106 * max: function(a, b) { return a > b ? a : b; }
1108 * @desc Adds two functions into the jQuery namespace
1111 * @param Object prop The object that will be merged into the jQuery object
1117 * Extend one object with one or more others, returning the original,
1118 * modified, object. This is a great utility for simple inheritance.
1120 * @example var settings = { validate: false, limit: 5, name: "foo" };
1121 * var options = { validate: true, name: "bar" };
1122 * jQuery.extend(settings, options);
1123 * @result settings == { validate: true, limit: 5, name: "bar" }
1124 * @desc Merge settings and options, modifying settings
1126 * @example var defaults = { validate: false, limit: 5, name: "foo" };
1127 * var options = { validate: true, name: "bar" };
1128 * var settings = jQuery.extend({}, defaults, options);
1129 * @result settings == { validate: true, limit: 5, name: "bar" }
1130 * @desc Merge defaults and options, without modifying the defaults
1133 * @param Object target The object to extend
1134 * @param Object prop1 The object that will be merged into the first.
1135 * @param Object propN (optional) More objects to merge into the first
1139 jQuery.extend = jQuery.fn.extend = function() {
1140 // copy reference to target object
1141 var target = arguments[0],
1144 // extend jQuery itself if only one argument is passed
1145 if ( arguments.length == 1 ) {
1150 while (prop = arguments[a++])
1151 // Extend the base object
1152 for ( var i in prop ) target[i] = prop[i];
1154 // Return the modified object
1160 * Run this function to give control of the $ variable back
1161 * to whichever library first implemented it. This helps to make
1162 * sure that jQuery doesn't conflict with the $ object
1163 * of other libraries.
1165 * By using this function, you will only be able to access jQuery
1166 * using the 'jQuery' variable. For example, where you used to do
1167 * $("div p"), you now must do jQuery("div p").
1169 * @example jQuery.noConflict();
1170 * // Do something with jQuery
1171 * jQuery("div p").hide();
1172 * // Do something with another library's $()
1173 * $("content").style.display = 'none';
1174 * @desc Maps the original object that was referenced by $ back to $
1176 * @example jQuery.noConflict();
1179 * // more code using $ as alias to jQuery
1182 * // other code using $ as an alias to the other library
1183 * @desc Reverts the $ alias and then creates and executes a
1184 * function to provide the $ as a jQuery alias inside the functions
1185 * scope. Inside the function the original $ object is not available.
1186 * This works well for most plugins that don't rely on any other library.
1189 * @name $.noConflict
1193 noConflict: function() {
1199 * A generic iterator function, which can be used to seemlessly
1200 * iterate over both objects and arrays. This function is not the same
1201 * as $().each() - which is used to iterate, exclusively, over a jQuery
1202 * object. This function can be used to iterate over anything.
1204 * The callback has two arguments:the key (objects) or index (arrays) as first
1205 * the first, and the value as the second.
1207 * @example $.each( [0,1,2], function(i, n){
1208 * alert( "Item #" + i + ": " + n );
1210 * @desc This is an example of iterating over the items in an array,
1211 * accessing both the current item and its index.
1213 * @example $.each( { name: "John", lang: "JS" }, function(i, n){
1214 * alert( "Name: " + i + ", Value: " + n );
1217 * @desc This is an example of iterating over the properties in an
1218 * Object, accessing both the current item and its key.
1221 * @param Object obj The object, or array, to iterate over.
1222 * @param Function fn The function that will be executed on every object.
1226 // args is for internal usage only
1227 each: function( obj, fn, args ) {
1228 if ( obj.length == undefined )
1229 for ( var i in obj )
1230 fn.apply( obj[i], args || [i, obj[i]] );
1232 for ( var i = 0, ol = obj.length; i < ol; i++ )
1233 if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break;
1238 add: function( elem, c ){
1239 jQuery.each( c.split(/\s+/), function(i, cur){
1240 if ( !jQuery.className.has( elem.className, cur ) )
1241 elem.className += ( elem.className ? " " : "" ) + cur;
1244 remove: function( elem, c ){
1245 elem.className = c ?
1246 jQuery.grep( elem.className.split(/\s+/), function(cur){
1247 return !jQuery.className.has( c, cur );
1250 has: function( classes, c ){
1251 return classes && new RegExp("(^|\\s)" + c + "(\\s|$)").test( classes );
1256 * Swap in/out style options.
1259 swap: function(e,o,f) {
1260 for ( var i in o ) {
1261 e.style["old"+i] = e.style[i];
1266 e.style[i] = e.style["old"+i];
1269 css: function(e,p) {
1270 if ( p == "height" || p == "width" ) {
1271 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
1273 for ( var i = 0, dl = d.length; i < dl; i++ ) {
1274 old["padding" + d[i]] = 0;
1275 old["border" + d[i] + "Width"] = 0;
1278 jQuery.swap( e, old, function() {
1279 if (jQuery.css(e,"display") != "none") {
1280 oHeight = e.offsetHeight;
1281 oWidth = e.offsetWidth;
1283 e = jQuery(e.cloneNode(true))
1284 .find(":radio").removeAttr("checked").end()
1286 visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
1287 }).appendTo(e.parentNode)[0];
1289 var parPos = jQuery.css(e.parentNode,"position");
1290 if ( parPos == "" || parPos == "static" )
1291 e.parentNode.style.position = "relative";
1293 oHeight = e.clientHeight;
1294 oWidth = e.clientWidth;
1296 if ( parPos == "" || parPos == "static" )
1297 e.parentNode.style.position = "static";
1299 e.parentNode.removeChild(e);
1303 return p == "height" ? oHeight : oWidth;
1306 return jQuery.curCSS( e, p );
1309 curCSS: function(elem, prop, force) {
1312 if (prop == 'opacity' && jQuery.browser.msie)
1313 return jQuery.attr(elem.style, 'opacity');
1315 if (prop == "float" || prop == "cssFloat")
1316 prop = jQuery.browser.msie ? "styleFloat" : "cssFloat";
1318 if (!force && elem.style[prop]) {
1320 ret = elem.style[prop];
1322 } else if (document.defaultView && document.defaultView.getComputedStyle) {
1324 if (prop == "cssFloat" || prop == "styleFloat")
1327 prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();
1328 var cur = document.defaultView.getComputedStyle(elem, null);
1331 ret = cur.getPropertyValue(prop);
1332 else if ( prop == 'display' )
1335 jQuery.swap(elem, { display: 'block' }, function() {
1336 var c = document.defaultView.getComputedStyle(this, '');
1337 ret = c && c.getPropertyValue(prop) || '';
1340 } else if (elem.currentStyle) {
1342 var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});
1343 ret = elem.currentStyle[prop] || elem.currentStyle[newProp];
1350 clean: function(a) {
1352 for ( var i = 0, al = a.length; i < al; i++ ) {
1354 if ( typeof arg == "string" ) { // Convert html string into DOM nodes
1355 // Trim whitespace, otherwise indexOf won't work as expected
1356 var s = jQuery.trim(arg), s3 = s.substring(0,3), s6 = s.substring(0,6),
1357 div = document.createElement("div"), wrap = [0,"",""];
1359 if ( s.substring(0,4) == "<opt" ) // option or optgroup
1360 wrap = [1, "<select>", "</select>"];
1361 else if ( s6 == "<thead" || s6 == "<tbody" || s6 == "<tfoot" )
1362 wrap = [1, "<table>", "</table>"];
1363 else if ( s3 == "<tr" )
1364 wrap = [2, "<table><tbody>", "</tbody></table>"];
1365 else if ( s3 == "<td" || s3 == "<th" ) // <thead> matched above
1366 wrap = [3, "<table><tbody><tr>", "</tr></tbody></table>"];
1368 // Go to html and back, then peel off extra wrappers
1369 div.innerHTML = wrap[1] + s + wrap[2];
1370 while ( wrap[0]-- ) div = div.firstChild;
1372 // Remove IE's autoinserted <tbody> from table fragments
1373 if ( jQuery.browser.msie ) {
1375 // String was a <table>, *may* have spurious <tbody>
1376 if ( s6 == "<table" && s.indexOf("<tbody") < 0 )
1377 tb = div.firstChild && div.firstChild.childNodes;
1378 // String was a bare <thead> or <tfoot>
1379 else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 )
1380 tb = div.childNodes;
1382 for ( var n = tb.length-1; n >= 0 ; --n )
1383 if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length )
1384 tb[n].parentNode.removeChild(tb[n]);
1388 arg = div.childNodes;
1392 if ( arg.length != undefined && ( (jQuery.browser.safari && typeof arg == 'function') || !arg.nodeType ) ) // Safari reports typeof on a DOM NodeList to be a function
1393 for ( var n = 0, argl = arg.length; n < argl; n++ ) // Handles Array, jQuery, DOM NodeList collections
1396 r.push( arg.nodeType ? arg : document.createTextNode(arg.toString()) );
1402 parseSetter: function(value) {
1403 if( typeof value == "string" && value.charAt(0) == "$" ) {
1404 var m = value.match(/{(.*)}$/);
1406 value = new Function( "return " + m[1] );
1412 attr: function(elem, name, value){
1415 "class": "className",
1416 "float": jQuery.browser.msie ? "styleFloat" : "cssFloat",
1417 cssFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",
1418 innerHTML: "innerHTML",
1419 className: "className",
1421 disabled: "disabled",
1423 readonly: "readOnly",
1424 selected: "selected"
1427 // get value if a function is provided
1428 if ( value && typeof value == "function" ) {
1429 value = value.apply( elem );
1432 // IE actually uses filters for opacity ... elem is actually elem.style
1433 if ( name == "opacity" && jQuery.browser.msie && value != undefined ) {
1434 // IE has trouble with opacity if it does not have layout
1435 // Force it by setting the zoom level
1438 // Set the alpha filter to set the opacity
1439 return elem.filter = elem.filter.replace(/alpha\([^\)]*\)/gi,"") +
1440 ( value == 1 ? "" : "alpha(opacity=" + value * 100 + ")" );
1442 } else if ( name == "opacity" && jQuery.browser.msie ) {
1443 return elem.filter ?
1444 parseFloat( elem.filter.match(/alpha\(opacity=(.*)\)/)[1] ) / 100 : 1;
1447 // Mozilla doesn't play well with opacity 1
1448 if ( name == "opacity" && jQuery.browser.mozilla && value == 1 )
1451 // Certain attributes only work when accessed via the old DOM 0 way
1453 if ( value != undefined ) elem[fix[name]] = value;
1454 return elem[fix[name]];
1456 } else if ( value == undefined && jQuery.browser.msie && elem.nodeName && elem.nodeName.toUpperCase() == 'FORM' && (name == 'action' || name == 'method') ) {
1457 return elem.getAttributeNode(name).nodeValue;
1459 // IE elem.getAttribute passes even for style
1460 } else if ( elem.tagName ) {
1461 if ( value != undefined ) elem.setAttribute( name, value );
1462 return elem.getAttribute( name );
1465 name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();});
1466 if ( value != undefined ) elem[name] = value;
1472 * Remove the whitespace from the beginning and end of a string.
1474 * @example $.trim(" hello, how are you? ");
1475 * @result "hello, how are you?"
1479 * @param String str The string to trim.
1483 return t.replace(/^\s+|\s+$/g, "");
1486 makeArray: function( a ) {
1489 if ( a.constructor != Array ) {
1490 for ( var i = 0, al = a.length; i < al; i++ )
1498 inArray: function( b, a ) {
1499 for ( var i = 0, al = a.length; i < al; i++ )
1506 * Merge two arrays together, removing all duplicates.
1508 * The new array is: All the results from the first array, followed
1509 * by the unique results from the second array.
1511 * @example $.merge( [0,1,2], [2,3,4] )
1512 * @result [0,1,2,3,4]
1513 * @desc Merges two arrays, removing the duplicate 2
1515 * @example $.merge( [3,2,1], [4,3,2] )
1517 * @desc Merges two arrays, removing the duplicates 3 and 2
1521 * @param Array first The first array to merge.
1522 * @param Array second The second array to merge.
1525 merge: function(first, second) {
1526 var r = [].slice.call( first, 0 );
1528 // Now check for duplicates between the two arrays
1529 // and only add the unique items
1530 for ( var i = 0, sl = second.length; i < sl; i++ ) {
1531 // Check for duplicates
1532 if ( jQuery.inArray( second[i], r ) == -1 )
1533 // The item is unique, add it
1534 first.push( second[i] );
1541 * Filter items out of an array, by using a filter function.
1543 * The specified function will be passed two arguments: The
1544 * current array item and the index of the item in the array. The
1545 * function must return 'true' to keep the item in the array,
1546 * false to remove it.
1548 * @example $.grep( [0,1,2], function(i){
1555 * @param Array array The Array to find items in.
1556 * @param Function fn The function to process each item against.
1557 * @param Boolean inv Invert the selection - select the opposite of the function.
1560 grep: function(elems, fn, inv) {
1561 // If a string is passed in for the function, make a function
1562 // for it (a handy shortcut)
1563 if ( typeof fn == "string" )
1564 fn = new Function("a","i","return " + fn);
1568 // Go through the array, only saving the items
1569 // that pass the validator function
1570 for ( var i = 0, el = elems.length; i < el; i++ )
1571 if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
1572 result.push( elems[i] );
1578 * Translate all items in an array to another array of items.
1580 * The translation function that is provided to this method is
1581 * called for each item in the array and is passed one argument:
1582 * The item to be translated.
1584 * The function can then return the translated value, 'null'
1585 * (to remove the item), or an array of values - which will
1586 * be flattened into the full array.
1588 * @example $.map( [0,1,2], function(i){
1592 * @desc Maps the original array to a new one and adds 4 to each value.
1594 * @example $.map( [0,1,2], function(i){
1595 * return i > 0 ? i + 1 : null;
1598 * @desc Maps the original array to a new one and adds 1 to each
1599 * value if it is bigger then zero, otherwise it's removed-
1601 * @example $.map( [0,1,2], function(i){
1602 * return [ i, i + 1 ];
1604 * @result [0, 1, 1, 2, 2, 3]
1605 * @desc Maps the original array to a new one, each element is added
1606 * with it's original value and the value plus one.
1610 * @param Array array The Array to translate.
1611 * @param Function fn The function to process each item against.
1614 map: function(elems, fn) {
1615 // If a string is passed in for the function, make a function
1616 // for it (a handy shortcut)
1617 if ( typeof fn == "string" )
1618 fn = new Function("a","return " + fn);
1620 var result = [], r = [];
1622 // Go through the array, translating each of the items to their
1623 // new value (or values).
1624 for ( var i = 0, el = elems.length; i < el; i++ ) {
1625 var val = fn(elems[i],i);
1627 if ( val !== null && val != undefined ) {
1628 if ( val.constructor != Array ) val = [val];
1629 result = result.concat( val );
1633 var r = result.length ? [ result[0] ] : [];
1635 check: for ( var i = 1, rl = result.length; i < rl; i++ ) {
1636 for ( var j = 0; j < i; j++ )
1637 if ( result[i] == r[j] )
1640 r.push( result[i] );
1648 * Contains flags for the useragent, read from navigator.userAgent.
1649 * Available flags are: safari, opera, msie, mozilla
1651 * This property is available before the DOM is ready, therefore you can
1652 * use it to add ready events only for certain browsers.
1654 * There are situations where object detections is not reliable enough, in that
1655 * cases it makes sense to use browser detection. Simply try to avoid both!
1657 * A combination of browser and object detection yields quite reliable results.
1659 * @example $.browser.msie
1660 * @desc Returns true if the current useragent is some version of microsoft's internet explorer
1662 * @example if($.browser.safari) { $( function() { alert("this is safari!"); } ); }
1663 * @desc Alerts "this is safari!" only for safari browsers
1672 * Wheather the W3C compliant box model is being used.
1680 var b = navigator.userAgent.toLowerCase();
1682 // Figure out what browser is being used
1684 safari: /webkit/.test(b),
1685 opera: /opera/.test(b),
1686 msie: /msie/.test(b) && !/opera/.test(b),
1687 mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
1690 // Check to see if the W3C box model is being used
1691 jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
1695 * Get a set of elements containing the unique parents of the matched
1698 * Can be filtered with an optional expressions.
1700 * @example $("p").parent()
1701 * @before <div><p>Hello</p><p>Hello</p></div>
1702 * @result [ <div><p>Hello</p><p>Hello</p></div> ]
1703 * @desc Find the parent element of each paragraph.
1705 * @example $("p").parent(".selected")
1706 * @before <div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>
1707 * @result [ <div class="selected"><p>Hello Again</p></div> ]
1708 * @desc Find the parent element of each paragraph with a class "selected".
1712 * @param String expr (optional) An expression to filter the parents with
1713 * @cat DOM/Traversing
1717 * Get a set of elements containing the unique ancestors of the matched
1718 * set of elements (except for the root element).
1720 * Can be filtered with an optional expressions.
1722 * @example $("span").parents()
1723 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
1724 * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
1725 * @desc Find all parent elements of each span.
1727 * @example $("span").parents("p")
1728 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
1729 * @result [ <p><span>Hello</span></p> ]
1730 * @desc Find all parent elements of each span that is a paragraph.
1734 * @param String expr (optional) An expression to filter the ancestors with
1735 * @cat DOM/Traversing
1739 * Get a set of elements containing the unique next siblings of each of the
1740 * matched set of elements.
1742 * It only returns the very next sibling, not all next siblings.
1744 * Can be filtered with an optional expressions.
1746 * @example $("p").next()
1747 * @before <p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
1748 * @result [ <p>Hello Again</p>, <div><span>And Again</span></div> ]
1749 * @desc Find the very next sibling of each paragraph.
1751 * @example $("p").next(".selected")
1752 * @before <p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>
1753 * @result [ <p class="selected">Hello Again</p> ]
1754 * @desc Find the very next sibling of each paragraph that has a class "selected".
1758 * @param String expr (optional) An expression to filter the next Elements with
1759 * @cat DOM/Traversing
1763 * Get a set of elements containing the unique previous siblings of each of the
1764 * matched set of elements.
1766 * Can be filtered with an optional expressions.
1768 * It only returns the immediately previous sibling, not all previous siblings.
1770 * @example $("p").prev()
1771 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
1772 * @result [ <div><span>Hello Again</span></div> ]
1773 * @desc Find the very previous sibling of each paragraph.
1775 * @example $("p").prev(".selected")
1776 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
1777 * @result [ <div><span>Hello</span></div> ]
1778 * @desc Find the very previous sibling of each paragraph that has a class "selected".
1782 * @param String expr (optional) An expression to filter the previous Elements with
1783 * @cat DOM/Traversing
1787 * Get a set of elements containing all of the unique siblings of each of the
1788 * matched set of elements.
1790 * Can be filtered with an optional expressions.
1792 * @example $("div").siblings()
1793 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
1794 * @result [ <p>Hello</p>, <p>And Again</p> ]
1795 * @desc Find all siblings of each div.
1797 * @example $("div").siblings(".selected")
1798 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
1799 * @result [ <p class="selected">Hello Again</p> ]
1800 * @desc Find all siblings with a class "selected" of each div.
1804 * @param String expr (optional) An expression to filter the sibling Elements with
1805 * @cat DOM/Traversing
1809 * Get a set of elements containing all of the unique children of each of the
1810 * matched set of elements.
1812 * Can be filtered with an optional expressions.
1814 * @example $("div").children()
1815 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
1816 * @result [ <span>Hello Again</span> ]
1817 * @desc Find all children of each div.
1819 * @example $("div").children(".selected")
1820 * @before <div><span>Hello</span><p class="selected">Hello Again</p><p>And Again</p></div>
1821 * @result [ <p class="selected">Hello Again</p> ]
1822 * @desc Find all children with a class "selected" of each div.
1826 * @param String expr (optional) An expression to filter the child Elements with
1827 * @cat DOM/Traversing
1830 parent: "a.parentNode",
1831 parents: jQuery.parents,
1832 next: "jQuery.nth(a,1,'nextSibling')",
1833 prev: "jQuery.nth(a,1,'previousSibling')",
1834 siblings: "jQuery.sibling(a.parentNode.firstChild,a)",
1835 children: "jQuery.sibling(a.firstChild)"
1837 jQuery.fn[ i ] = function(a) {
1838 var ret = jQuery.map(this,n);
1839 if ( a && typeof a == "string" )
1840 ret = jQuery.filter(a,ret).r;
1841 return this.set( ret );
1846 * Append all of the matched elements to another, specified, set of elements.
1847 * This operation is, essentially, the reverse of doing a regular
1848 * $(A).append(B), in that instead of appending B to A, you're appending
1851 * @example $("p").appendTo("#foo");
1852 * @before <p>I would like to say: </p><div id="foo"></div>
1853 * @result <div id="foo"><p>I would like to say: </p></div>
1854 * @desc Appends all paragraphs to the element with the ID "foo"
1858 * @param String expr A jQuery expression of elements to match.
1859 * @cat DOM/Manipulation
1863 * Prepend all of the matched elements to another, specified, set of elements.
1864 * This operation is, essentially, the reverse of doing a regular
1865 * $(A).prepend(B), in that instead of prepending B to A, you're prepending
1868 * @example $("p").prependTo("#foo");
1869 * @before <p>I would like to say: </p><div id="foo"><b>Hello</b></div>
1870 * @result <div id="foo"><p>I would like to say: </p><b>Hello</b></div>
1871 * @desc Prepends all paragraphs to the element with the ID "foo"
1875 * @param String expr A jQuery expression of elements to match.
1876 * @cat DOM/Manipulation
1880 * Insert all of the matched elements before another, specified, set of elements.
1881 * This operation is, essentially, the reverse of doing a regular
1882 * $(A).before(B), in that instead of inserting B before A, you're inserting
1885 * @example $("p").insertBefore("#foo");
1886 * @before <div id="foo">Hello</div><p>I would like to say: </p>
1887 * @result <p>I would like to say: </p><div id="foo">Hello</div>
1888 * @desc Same as $("#foo").before("p")
1890 * @name insertBefore
1892 * @param String expr A jQuery expression of elements to match.
1893 * @cat DOM/Manipulation
1897 * Insert all of the matched elements after another, specified, set of elements.
1898 * This operation is, essentially, the reverse of doing a regular
1899 * $(A).after(B), in that instead of inserting B after A, you're inserting
1902 * @example $("p").insertAfter("#foo");
1903 * @before <p>I would like to say: </p><div id="foo">Hello</div>
1904 * @result <div id="foo">Hello</div><p>I would like to say: </p>
1905 * @desc Same as $("#foo").after("p")
1909 * @param String expr A jQuery expression of elements to match.
1910 * @cat DOM/Manipulation
1915 prependTo: "prepend",
1916 insertBefore: "before",
1917 insertAfter: "after"
1919 jQuery.fn[ i ] = function(){
1921 return this.each(function(){
1922 for ( var j = 0, al = a.length; j < al; j++ )
1923 jQuery(a[j])[n]( this );
1929 * Remove an attribute from each of the matched elements.
1931 * @example $("input").removeAttr("disabled")
1932 * @before <input disabled="disabled"/>
1937 * @param String name The name of the attribute to remove.
1938 * @cat DOM/Attributes
1942 * Displays each of the set of matched elements if they are hidden.
1944 * @example $("p").show()
1945 * @before <p style="display: none">Hello</p>
1946 * @result [ <p style="display: block">Hello</p> ]
1954 * Hides each of the set of matched elements if they are shown.
1956 * @example $("p").hide()
1957 * @before <p>Hello</p>
1958 * @result [ <p style="display: none">Hello</p> ]
1960 * var pass = true, div = $("div");
1961 * div.hide().each(function(){
1962 * if ( this.style.display != "none" ) pass = false;
1964 * ok( pass, "Hide" );
1972 * Toggles each of the set of matched elements. If they are shown,
1973 * toggle makes them hidden. If they are hidden, toggle
1976 * @example $("p").toggle()
1977 * @before <p>Hello</p><p style="display: none">Hello Again</p>
1978 * @result [ <p style="display: none">Hello</p>, <p style="display: block">Hello Again</p> ]
1986 * Adds the specified class to each of the set of matched elements.
1988 * @example $("p").addClass("selected")
1989 * @before <p>Hello</p>
1990 * @result [ <p class="selected">Hello</p> ]
1994 * @param String class A CSS class to add to the elements
1995 * @cat DOM/Attributes
1996 * @see removeClass(String)
2000 * Removes all or the specified class from the set of matched elements.
2002 * @example $("p").removeClass()
2003 * @before <p class="selected">Hello</p>
2004 * @result [ <p>Hello</p> ]
2006 * @example $("p").removeClass("selected")
2007 * @before <p class="selected first">Hello</p>
2008 * @result [ <p class="first">Hello</p> ]
2012 * @param String class (optional) A CSS class to remove from the elements
2013 * @cat DOM/Attributes
2014 * @see addClass(String)
2018 * Adds the specified class if it is not present, removes it if it is
2021 * @example $("p").toggleClass("selected")
2022 * @before <p>Hello</p><p class="selected">Hello Again</p>
2023 * @result [ <p class="selected">Hello</p>, <p>Hello Again</p> ]
2027 * @param String class A CSS class with which to toggle the elements
2028 * @cat DOM/Attributes
2032 * Removes all matched elements from the DOM. This does NOT remove them from the
2033 * jQuery object, allowing you to use the matched elements further.
2035 * Can be filtered with an optional expressions.
2037 * @example $("p").remove();
2038 * @before <p>Hello</p> how are <p>you?</p>
2041 * @example $("p").remove(".hello");
2042 * @before <p class="hello">Hello</p> how are <p>you?</p>
2043 * @result how are <p>you?</p>
2047 * @param String expr (optional) A jQuery expression to filter elements by.
2048 * @cat DOM/Manipulation
2052 * Removes all child nodes from the set of matched elements.
2054 * @example $("p").empty()
2055 * @before <p>Hello, <span>Person</span> <a href="#">and person</a></p>
2056 * @result [ <p></p> ]
2060 * @cat DOM/Manipulation
2064 removeAttr: function( key ) {
2065 jQuery.attr( this, key, "" );
2066 this.removeAttribute( key );
2069 this.style.display = this.oldblock ? this.oldblock : "";
2070 if ( jQuery.css(this,"display") == "none" )
2071 this.style.display = "block";
2074 this.oldblock = this.oldblock || jQuery.css(this,"display");
2075 if ( this.oldblock == "none" )
2076 this.oldblock = "block";
2077 this.style.display = "none";
2080 jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ].apply( jQuery(this), arguments );
2082 addClass: function(c){
2083 jQuery.className.add(this,c);
2085 removeClass: function(c){
2086 jQuery.className.remove(this,c);
2088 toggleClass: function( c ){
2089 jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this, c);
2091 remove: function(a){
2092 if ( !a || jQuery.filter( a, [this] ).r )
2093 this.parentNode.removeChild( this );
2096 while ( this.firstChild )
2097 this.removeChild( this.firstChild );
2100 jQuery.fn[ i ] = function() {
2101 return this.each( n, arguments );
2106 * Reduce the set of matched elements to a single element.
2107 * The position of the element in the set of matched elements
2108 * starts at 0 and goes to length - 1.
2110 * @example $("p").eq(1)
2111 * @before <p>This is just a test.</p><p>So is this</p>
2112 * @result [ <p>So is this</p> ]
2116 * @param Number pos The index of the element that you wish to limit to.
2121 * Reduce the set of matched elements to all elements before a given position.
2122 * The position of the element in the set of matched elements
2123 * starts at 0 and goes to length - 1.
2125 * @example $("p").lt(1)
2126 * @before <p>This is just a test.</p><p>So is this</p>
2127 * @result [ <p>This is just a test.</p> ]
2131 * @param Number pos Reduce the set to all elements below this position.
2136 * Reduce the set of matched elements to all elements after a given position.
2137 * The position of the element in the set of matched elements
2138 * starts at 0 and goes to length - 1.
2140 * @example $("p").gt(0)
2141 * @before <p>This is just a test.</p><p>So is this</p>
2142 * @result [ <p>So is this</p> ]
2146 * @param Number pos Reduce the set to all elements after this position.
2151 * Filter the set of elements to those that contain the specified text.
2153 * @example $("p").contains("test")
2154 * @before <p>This is just a test.</p><p>So is this</p>
2155 * @result [ <p>This is just a test.</p> ]
2159 * @param String str The string that will be contained within the text of an element.
2160 * @cat DOM/Traversing
2162 jQuery.each( [ "eq", "lt", "gt", "contains" ], function(i,n){
2163 jQuery.fn[ n ] = function(num,fn) {
2164 return this.filter( ":" + n + "(" + num + ")", fn );