2 * jQuery - 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
18 * @test ok( Array.prototype.push, "Array.push()" );
19 * @test ok( Function.prototype.apply, "Function.apply()" );
20 * @test ok( document.getElementById, "getElementById" );
21 * @test ok( document.getElementsByTagName, "getElementsByTagName" );
22 * @test ok( RegExp, "RegExp" );
23 * @test ok( jQuery, "jQuery" );
24 * @test ok( $, "$()" );
31 function jQuery(a,c) {
33 // Shortcut for document ready (because $(document).each() is silly)
34 if ( a && a.constructor == Function && jQuery.fn.ready )
35 return jQuery(document).ready(a);
37 // Make sure that a selection was provided
38 a = a || jQuery.context || document;
40 // Watch for when a jQuery object is passed as the selector
42 return jQuery( jQuery.merge( a, [] ) );
44 // Watch for when a jQuery object is passed at the context
46 return jQuery( c ).find(a);
48 // If the context is global, return a new object
50 return new jQuery(a,c);
52 // Handle HTML strings
53 var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
54 if ( m ) a = jQuery.clean( [ m[1] ] );
56 // Watch for when an array is passed in
57 this.get( a.constructor == Array || a.length && !a.nodeType && a[0] != undefined && a[0].nodeType ?
58 // Assume that it is an array of DOM Elements
59 jQuery.merge( a, [] ) :
61 // Find the matching elements and save them for later
62 jQuery.find( a, c ) );
64 // See if an extra function was provided
65 var fn = arguments[ arguments.length - 1 ];
67 // If so, execute it in context
68 if ( fn && fn.constructor == Function )
72 // Map over the $ in case of overwrite
73 if ( typeof $ != "undefined" )
77 * This function accepts a string containing a CSS selector,
78 * basic XPath, or raw HTML, which is then used to match a set of elements.
79 * The HTML string is different from the traditional selectors in that
80 * it creates the DOM elements representing that HTML string, on the fly,
81 * to be (assumedly) inserted into the document later.
83 * The core functionality of jQuery centers around this function.
84 * Everything in jQuery is based upon this, or uses this in some way.
85 * The most basic use of this function is to pass in an expression
86 * (usually consisting of CSS or XPath), which then finds all matching
87 * elements and remembers them for later use.
89 * By default, $() looks for DOM elements within the context of the
90 * current HTML document.
92 * @example $("div > p")
93 * @desc This finds all p elements that are children of a div element.
94 * @before <p>one</p> <div><p>two</p></div> <p>three</p>
95 * @result [ <p>two</p> ]
97 * @example $("<div><p>Hello</p></div>").appendTo("#body")
98 * @desc Creates a div element (and all of its contents) dynamically, and appends it to the element with the ID of body.
101 * @param String expr An expression to search with, or a string of HTML to create on the fly.
107 * This function accepts a string containing a CSS selector, or
108 * basic XPath, which is then used to match a set of elements with the
109 * context of the specified DOM element, or document
111 * @example $("div", xml.responseXML)
112 * @desc This finds all div elements within the specified XML document.
115 * @param String expr An expression to search with.
116 * @param Element context A DOM Element, or Document, representing the base context.
122 * Wrap jQuery functionality around a specific DOM Element.
123 * This function also accepts XML Documents and Window objects
124 * as valid arguments (even though they are not DOM Elements).
126 * @example $(document).find("div > p")
127 * @before <p>one</p> <div><p>two</p></div> <p>three</p>
128 * @result [ <p>two</p> ]
130 * @example $(document.body).background( "black" );
131 * @desc Sets the background color of the page to black.
134 * @param Element elem A DOM element to be encapsulated by a jQuery object.
140 * Wrap jQuery functionality around a set of DOM Elements.
142 * @example $( myForm.elements ).hide()
143 * @desc Hides all the input elements within a form
146 * @param Array<Element> elems An array of DOM elements to be encapsulated by a jQuery object.
152 * A shorthand for $(document).ready(), allowing you to bind a function
153 * to be executed when the DOM document has finished loading. This function
154 * behaves just like $(document).ready(), in that it should be used to wrap
155 * all of the other $() operations on your page. While this function is,
156 * technically, chainable - there really isn't much use for chaining against it.
158 * @example $(function(){
159 * // Document is ready
161 * @desc Executes the function when the DOM is ready to be used.
164 * @param Function fn The function to execute when the DOM is ready.
170 * A means of creating a cloned copy of a jQuery object. This function
171 * copies the set of matched elements from one jQuery object and creates
172 * another, new, jQuery object containing the same elements.
174 * @example var div = $("div");
175 * $( div ).find("p");
176 * @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).
179 * @param jQuery obj The jQuery object to be cloned.
184 // Map the jQuery namespace to the '$' one
187 jQuery.fn = jQuery.prototype = {
189 * The current SVN version of jQuery.
200 * The number of elements currently matched.
202 * @example $("img").length;
203 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
206 * @test cmpOK( $("div").length, "==", 2, "Get Number of Elements Found" );
215 * The number of elements currently matched.
217 * @example $("img").size();
218 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
221 * @test cmpOK( $("div").size(), "==", 2, "Get Number of Elements Found" );
232 * Access all matched elements. This serves as a backwards-compatible
233 * way of accessing all matched elements (other than the jQuery object
234 * itself, which is, in fact, an array of elements).
236 * @example $("img").get();
237 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
238 * @result [ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
240 * @test isSet( $("div").get(), q("main","foo"), "Get All Elements" );
243 * @type Array<Element>
248 * Access a single matched element. num is used to access the
249 * Nth element matched.
251 * @example $("img").get(1);
252 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
253 * @result [ <img src="test1.jpg"/> ]
255 * @test cmpOK( $("div").get(0), "==", document.getElementById("main"), "Get A Single Element" );
259 * @param Number num Access the element in the Nth position.
264 * Set the jQuery object to an array of elements.
266 * @example $("img").get([ document.body ]);
267 * @result $("img").get() == [ document.body ]
272 * @param Elements elems An array of elements
275 get: function( num ) {
276 // Watch for when an array (of elements) is passed in
277 if ( num && num.constructor == Array ) {
279 // Use a tricky hack to make the jQuery object
280 // look and feel like an array
282 [].push.apply( this, num );
286 return num == undefined ?
288 // Return a 'clean' array
289 jQuery.merge( this, [] ) :
291 // Return just the object
296 * Execute a function within the context of every matched element.
297 * This means that every time the passed-in function is executed
298 * (which is once for every element matched) the 'this' keyword
299 * points to the specific element.
301 * Additionally, the function, when executed, is passed a single
302 * argument representing the position of the element in the matched
305 * @example $("img").each(function(){
306 * this.src = "test.jpg";
308 * @before <img/> <img/>
309 * @result <img src="test.jpg"/> <img src="test.jpg"/>
311 * @example $("img").each(function(i){
312 * alert( "Image #" + i + " is " + this );
314 * @before <img/> <img/>
315 * @result <img src="test.jpg"/> <img src="test.jpg"/>
317 * @test var div = $("div");
318 * div.each(function(){this.foo = 'zoo';});
320 * for ( var i = 0; i < div.size(); i++ ) {
321 * if ( div.get(i).foo != "zoo" ) pass = false;
323 * ok( pass, "Execute a function, Relative" );
327 * @param Function fn A function to execute
330 each: function( fn, args ) {
331 return jQuery.each( this, fn, args );
335 * Searches every matched element for the object and returns
336 * the index of the element, if found, starting with zero.
337 * Returns -1 if the object wasn't found.
339 * @example $("*").index(document.getElementById('foobar'))
340 * @before <div id="foobar"></div><b></b><span id="foo"></span>
343 * @example $("*").index(document.getElementById('foo'))
344 * @before <div id="foobar"></div><b></b><span id="foo"></span>
347 * @example $("*").index(document.getElementById('bar'))
348 * @before <div id="foobar"></div><b></b><span id="foo"></span>
351 * @test ok( $([window, document]).index(window) == 0, "Check for index of elements" );
352 * @test ok( $([window, document]).index(document) == 1, "Check for index of elements" );
353 * @test var inputElements = $('#radio1,#radio2,#check1,#check2');
354 * @test ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
355 * @test ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
356 * @test ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
357 * @test ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
358 * @test ok( inputElements.index(window) == -1, "Check for not found index" );
359 * @test ok( inputElements.index(document) == -1, "Check for not found index" );
363 * @param Object obj Object to search for
366 index: function( obj ) {
368 this.each(function(i){
369 if ( this == obj ) pos = i;
375 * Access a property on the first matched element.
376 * This method makes it easy to retrieve a property value
377 * from the first matched element.
379 * @example $("img").attr("src");
380 * @before <img src="test.jpg"/>
383 * @test ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
384 * @test ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
385 * @test ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
386 * @test ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
387 * @test ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
388 * @test ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
389 * @test ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
390 * @test ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
391 * @test ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
392 * @test ok( $('#name').attr('name') == "name", 'Check for name attribute' );
393 * @test ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
394 * @test ok( $('#form').attr('action') == "formaction", 'Check for action attribute' );
398 * @param String name The name of the property to access.
403 * Set a hash of key/value object properties to all matched elements.
404 * This serves as the best way to set a large number of properties
405 * on all matched elements.
407 * @example $("img").attr({ src: "test.jpg", alt: "Test Image" });
409 * @result <img src="test.jpg" alt="Test Image"/>
411 * @test var pass = true;
412 * $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
413 * if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
415 * ok( pass, "Set Multiple Attributes" );
419 * @param Hash prop A set of key/value pairs to set as object properties.
424 * Set a single property to a value, on all matched elements.
426 * @example $("img").attr("src","test.jpg");
428 * @result <img src="test.jpg"/>
430 * @test var div = $("div");
431 * div.attr("foo", "bar");
433 * for ( var i = 0; i < div.size(); i++ ) {
434 * if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
436 * ok( pass, "Set Attribute" );
438 * @test $("#name").attr('name', 'something');
439 * ok( $("#name").name() == 'something', 'Set name attribute' );
440 * @test $("#check2").attr('checked', true);
441 * ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
442 * $("#check2").attr('checked', false);
443 * ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
447 * @param String key The name of the property to set.
448 * @param Object value The value to set the property to.
451 attr: function( key, value, type ) {
452 // Check to see if we're setting style values
453 return key.constructor != String || value != undefined ?
454 this.each(function(){
455 // See if we're setting a hash of styles
456 if ( value == undefined )
457 // Set all the styles
458 for ( var prop in key )
460 type ? this.style : this,
464 // See if we're setting a single key/value style
467 type ? this.style : this,
472 // Look for the case where we're accessing a style value
473 jQuery[ type || "attr" ]( this[0], key );
477 * Access a style property on the first matched element.
478 * This method makes it easy to retrieve a style property value
479 * from the first matched element.
481 * @example $("p").css("color");
482 * @before <p style="color:red;">Test Paragraph.</p>
484 * @desc Retrieves the color style of the first paragraph
486 * @example $("p").css("fontWeight");
487 * @before <p style="font-weight: bold;">Test Paragraph.</p>
489 * @desc Retrieves the font-weight style of the first paragraph.
490 * Note that for all style properties with a dash (like 'font-weight'), you have to
491 * write it in camelCase. In other words: Every time you have a '-' in a
492 * property, remove it and replace the next character with an uppercase
493 * representation of itself. Eg. fontWeight, fontSize, fontFamily, borderWidth,
494 * borderStyle, borderBottomWidth etc.
496 * @test ok( $('#foo').css("display") == 'block', 'Check for css property "display"');
500 * @param String name The name of the property to access.
505 * Set a hash of key/value style properties to all matched elements.
506 * This serves as the best way to set a large number of style properties
507 * on all matched elements.
509 * @example $("p").css({ color: "red", background: "blue" });
510 * @before <p>Test Paragraph.</p>
511 * @result <p style="color:red; background:blue;">Test Paragraph.</p>
513 * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
514 * @test $('#foo').css({display: 'none'});
515 * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
516 * @test $('#foo').css({display: 'block'});
517 * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
521 * @param Hash prop A set of key/value pairs to set as style properties.
526 * Set a single style property to a value, on all matched elements.
528 * @example $("p").css("color","red");
529 * @before <p>Test Paragraph.</p>
530 * @result <p style="color:red;">Test Paragraph.</p>
531 * @desc Changes the color of all paragraphs to red
534 * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
535 * @test $('#foo').css('display', 'none');
536 * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
537 * @test $('#foo').css('display', 'block');
538 * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
542 * @param String key The name of the property to set.
543 * @param Object value The value to set the property to.
546 css: function( key, value ) {
547 return this.attr( key, value, "curCSS" );
551 * Retrieve the text contents of all matched elements. The result is
552 * a string that contains the combined text contents of all matched
553 * elements. This method works on both HTML and XML documents.
555 * @example $("p").text();
556 * @before <p>Test Paragraph.</p>
557 * @result Test Paragraph.
559 * @test var expected = "This link has class=\"blog\": Simon Willison's Weblog";
560 * ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
569 for ( var j = 0; j < e.length; j++ ) {
570 var r = e[j].childNodes;
571 for ( var i = 0; i < r.length; i++ )
572 if ( r[i].nodeType != 8 )
573 t += r[i].nodeType != 1 ?
574 r[i].nodeValue : jQuery.fn.text([ r[i] ]);
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 * @example $("p").wrap("<div class='wrap'></div>");
591 * @before <p>Test Paragraph.</p>
592 * @result <div class='wrap'><p>Test Paragraph.</p></div>
594 * @test var defaultText = 'Try them out:'
595 * var result = $('#first').wrap('<div class="red"><span></span></div>').text();
596 * ok( defaultText == result, 'Check for simple wrapping' );
597 * ok( $('#first').parent().parent().is('.red'), 'Check if wrapper div has class "red"' );
599 * @test var defaultText = 'Try them out:'
600 * var result = $('#first').wrap('<div class="red">xx<span></span>yy</div>').text()
601 * ok( 'xx' + defaultText + 'yy' == result, 'Check for wrapping' );
602 * ok( $('#first').parent().parent().is('.red'), 'Check if wrapper div has class "red"' );
606 * @param String html A string of HTML, that will be created on the fly and wrapped around the target.
607 * @cat DOM/Manipulation
611 * Wrap all matched elements with a structure of other elements.
612 * This wrapping process is most useful for injecting additional
613 * stucture into a document, without ruining the original semantic
614 * qualities of a document.
616 * This works by going through the first element
617 * provided and finding the deepest ancestor element within its
618 * structure - it is that element that will en-wrap everything else.
620 * @example $("p").wrap("<div class='wrap'></div>");
621 * @before <p>Test Paragraph.</p>
622 * @result <div class='wrap'><p>Test Paragraph.</p></div>
626 * @param Element elem A DOM element that will be wrapped.
627 * @cat DOM/Manipulation
630 // The elements to wrap the target around
631 var a = jQuery.clean(arguments);
633 // Wrap each of the matched elements individually
634 return this.each(function(){
635 // Clone the structure that we're using to wrap
636 var b = a[0].cloneNode(true);
638 // Insert it before the element to be wrapped
639 this.parentNode.insertBefore( b, this );
641 // Find he deepest point in the wrap structure
642 while ( b.firstChild )
645 // Move the matched element to within the wrap structure
646 b.appendChild( this );
651 * Append any number of elements to the inside of every matched elements,
652 * generated from the provided HTML.
653 * This operation is similar to doing an appendChild to all the
654 * specified elements, adding them into the document.
656 * @example $("p").append("<b>Hello</b>");
657 * @before <p>I would like to say: </p>
658 * @result <p>I would like to say: <b>Hello</b></p>
660 * @test var defaultText = 'Try them out:'
661 * var result = $('#first').append('<b>buga</b>');
662 * ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
666 * @param String html A string of HTML, that will be created on the fly and appended to the target.
667 * @cat DOM/Manipulation
671 * Append an element to the inside of all matched elements.
672 * This operation is similar to doing an appendChild to all the
673 * specified elements, adding them into the document.
675 * @example $("p").append( $("#foo")[0] );
676 * @before <p>I would like to say: </p><b id="foo">Hello</b>
677 * @result <p>I would like to say: <b id="foo">Hello</b></p>
679 * @test var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
680 * $('#sap').append(document.getElementById('first'));
681 * ok( expected == $('#sap').text(), "Check for appending of element" );
685 * @param Element elem A DOM element that will be appended.
686 * @cat DOM/Manipulation
690 * Append any number of elements to the inside of all matched elements.
691 * This operation is similar to doing an appendChild to all the
692 * specified elements, adding them into the document.
694 * @example $("p").append( $("b") );
695 * @before <p>I would like to say: </p><b>Hello</b>
696 * @result <p>I would like to say: <b>Hello</b></p>
698 * @test var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
699 * $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
700 * ok( expected == $('#sap').text(), "Check for appending of array of elements" );
704 * @param Array<Element> elems An array of elements, all of which will be appended.
705 * @cat DOM/Manipulation
708 return this.domManip(arguments, true, 1, function(a){
709 this.appendChild( a );
714 * Prepend any number of elements to the inside of every matched elements,
715 * generated from the provided HTML.
716 * This operation is the best way to insert dynamically created elements
717 * inside, at the beginning, of all the matched element.
719 * @example $("p").prepend("<b>Hello</b>");
720 * @before <p>I would like to say: </p>
721 * @result <p><b>Hello</b>I would like to say: </p>
723 * @test var defaultText = 'Try them out:'
724 * var result = $('#first').prepend('<b>buga</b>');
725 * ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
729 * @param String html A string of HTML, that will be created on the fly and appended to the target.
730 * @cat DOM/Manipulation
734 * Prepend an element to the inside of all matched elements.
735 * This operation is the best way to insert an element inside, at the
736 * beginning, of all the matched element.
738 * @example $("p").prepend( $("#foo")[0] );
739 * @before <p>I would like to say: </p><b id="foo">Hello</b>
740 * @result <p><b id="foo">Hello</b>I would like to say: </p>
742 * @test var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
743 * $('#sap').prepend(document.getElementById('first'));
744 * ok( expected == $('#sap').text(), "Check for prepending of element" );
748 * @param Element elem A DOM element that will be appended.
749 * @cat DOM/Manipulation
753 * Prepend any number of elements to the inside of all matched elements.
754 * This operation is the best way to insert a set of elements inside, at the
755 * beginning, of all the matched element.
757 * @example $("p").prepend( $("b") );
758 * @before <p>I would like to say: </p><b>Hello</b>
759 * @result <p><b>Hello</b>I would like to say: </p>
761 * @test var expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
762 * $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
763 * ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
767 * @param Array<Element> elems An array of elements, all of which will be appended.
768 * @cat DOM/Manipulation
770 prepend: function() {
771 return this.domManip(arguments, true, -1, function(a){
772 this.insertBefore( a, this.firstChild );
777 * Insert any number of dynamically generated elements before each of the
780 * @example $("p").before("<b>Hello</b>");
781 * @before <p>I would like to say: </p>
782 * @result <b>Hello</b><p>I would like to say: </p>
784 * @test var expected = 'This is a normal link: bugaYahoo';
785 * $('#yahoo').before('<b>buga</b>');
786 * ok( expected == $('#en').text(), 'Insert String before' );
790 * @param String html A string of HTML, that will be created on the fly and appended to the target.
791 * @cat DOM/Manipulation
795 * Insert an element before each of the matched elements.
797 * @example $("p").before( $("#foo")[0] );
798 * @before <p>I would like to say: </p><b id="foo">Hello</b>
799 * @result <b id="foo">Hello</b><p>I would like to say: </p>
801 * @test var expected = "This is a normal link: Try them out:Yahoo";
802 * $('#yahoo').before(document.getElementById('first'));
803 * ok( expected == $('#en').text(), "Insert element before" );
807 * @param Element elem A DOM element that will be appended.
808 * @cat DOM/Manipulation
812 * Insert any number of elements before each of the matched elements.
814 * @example $("p").before( $("b") );
815 * @before <p>I would like to say: </p><b>Hello</b>
816 * @result <b>Hello</b><p>I would like to say: </p>
818 * @test var expected = "This is a normal link: Try them out:diveintomarkYahoo";
819 * $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
820 * ok( expected == $('#en').text(), "Insert array of elements before" );
824 * @param Array<Element> elems An array of elements, all of which will be appended.
825 * @cat DOM/Manipulation
828 return this.domManip(arguments, false, 1, function(a){
829 this.parentNode.insertBefore( a, this );
834 * Insert any number of dynamically generated elements after each of the
837 * @example $("p").after("<b>Hello</b>");
838 * @before <p>I would like to say: </p>
839 * @result <p>I would like to say: </p><b>Hello</b>
841 * @test var expected = 'This is a normal link: Yahoobuga';
842 * $('#yahoo').after('<b>buga</b>');
843 * ok( expected == $('#en').text(), 'Insert String after' );
847 * @param String html A string of HTML, that will be created on the fly and appended to the target.
848 * @cat DOM/Manipulation
852 * Insert an element after each of the matched elements.
854 * @example $("p").after( $("#foo")[0] );
855 * @before <b id="foo">Hello</b><p>I would like to say: </p>
856 * @result <p>I would like to say: </p><b id="foo">Hello</b>
858 * @test var expected = "This is a normal link: YahooTry them out:";
859 * $('#yahoo').after(document.getElementById('first'));
860 * ok( expected == $('#en').text(), "Insert element after" );
864 * @param Element elem A DOM element that will be appended.
865 * @cat DOM/Manipulation
869 * Insert any number of elements after each of the matched elements.
871 * @example $("p").after( $("b") );
872 * @before <b>Hello</b><p>I would like to say: </p>
873 * @result <p>I would like to say: </p><b>Hello</b>
875 * @test var expected = "This is a normal link: YahooTry them out:diveintomark";
876 * $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
877 * ok( expected == $('#en').text(), "Insert array of elements after" );
881 * @param Array<Element> elems An array of elements, all of which will be appended.
882 * @cat DOM/Manipulation
885 return this.domManip(arguments, false, -1, function(a){
886 this.parentNode.insertBefore( a, this.nextSibling );
891 * End the most recent 'destructive' operation, reverting the list of matched elements
892 * back to its previous state. After an end operation, the list of matched elements will
893 * revert to the last state of matched elements.
895 * @example $("p").find("span").end();
896 * @before <p><span>Hello</span>, how are you?</p>
897 * @result $("p").find("span").end() == [ <p>...</p> ]
899 * @test ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
903 * @cat DOM/Traversing
906 return this.get( this.stack.pop() );
910 * Searches for all elements that match the specified expression.
911 * This method is the optimal way of finding additional descendant
912 * elements with which to process.
914 * All searching is done using a jQuery expression. The expression can be
915 * written using CSS 1-3 Selector syntax, or basic XPath.
917 * @example $("p").find("span");
918 * @before <p><span>Hello</span>, how are you?</p>
919 * @result $("p").find("span") == [ <span>Hello</span> ]
921 * @test ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
925 * @param String expr An expression to search with.
926 * @cat DOM/Traversing
929 return this.pushStack( jQuery.map( this, function(a){
930 return jQuery.find(t,a);
935 * Create cloned copies of all matched DOM Elements. This does
936 * not create a cloned copy of this particular jQuery object,
937 * instead it creates duplicate copies of all DOM Elements.
938 * This is useful for moving copies of the elements to another
939 * location in the DOM.
941 * @example $("b").clone().prependTo("p");
942 * @before <b>Hello</b><p>, how are you?</p>
943 * @result <b>Hello</b><p><b>Hello</b>, how are you?</p>
945 * @test ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
946 * var clone = $('#yahoo').clone();
947 * ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
948 * ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
952 * @cat DOM/Manipulation
954 clone: function(deep) {
955 return this.pushStack( jQuery.map( this, function(a){
956 return a.cloneNode( deep != undefined ? deep : true );
961 * Removes all elements from the set of matched elements that do not
962 * match the specified expression. This method is used to narrow down
963 * the results of a search.
965 * All searching is done using a jQuery expression. The expression
966 * can be written using CSS 1-3 Selector syntax, or basic XPath.
968 * @example $("p").filter(".selected")
969 * @before <p class="selected">Hello</p><p>How are you?</p>
970 * @result $("p").filter(".selected") == [ <p class="selected">Hello</p> ]
972 * @test isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "Filter elements" );
976 * @param String expr An expression to search with.
977 * @cat DOM/Traversing
981 * Removes all elements from the set of matched elements that do not
982 * match at least one of the expressions passed to the function. This
983 * method is used when you want to filter the set of matched elements
984 * through more than one expression.
986 * Elements will be retained in the jQuery object if they match at
987 * least one of the expressions passed.
989 * @example $("p").filter([".selected", ":first"])
990 * @before <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
991 * @result $("p").filter([".selected", ":first"]) == [ <p>Hello</p>, <p class="selected">And Again</p> ]
995 * @param Array<String> exprs A set of expressions to evaluate against
996 * @cat DOM/Traversing
998 filter: function(t) {
999 return this.pushStack(
1000 t.constructor == Array &&
1001 jQuery.map(this,function(a){
1002 for ( var i = 0; i < t.length; i++ )
1003 if ( jQuery.filter(t[i],[a]).r.length )
1007 t.constructor == Boolean &&
1008 ( t ? this.get() : [] ) ||
1010 t.constructor == Function &&
1011 jQuery.grep( this, t ) ||
1013 jQuery.filter(t,this).r, arguments );
1017 * Removes the specified Element from the set of matched elements. This
1018 * method is used to remove a single Element from a jQuery object.
1020 * @example $("p").not( document.getElementById("selected") )
1021 * @before <p>Hello</p><p id="selected">Hello Again</p>
1022 * @result [ <p>Hello</p> ]
1026 * @param Element el An element to remove from the set
1027 * @cat DOM/Traversing
1031 * Removes elements matching the specified expression from the set
1032 * of matched elements. This method is used to remove one or more
1033 * elements from a jQuery object.
1035 * @example $("p").not("#selected")
1036 * @before <p>Hello</p><p id="selected">Hello Again</p>
1037 * @result [ <p>Hello</p> ]
1038 * @test cmpOK($("#main > p#ap > a").not("#google").length, "==", 2, ".not")
1042 * @param String expr An expression with which to remove matching elements
1043 * @cat DOM/Traversing
1046 return this.pushStack( t.constructor == String ?
1047 jQuery.filter(t,this,false).r :
1048 jQuery.grep(this,function(a){ return a != t; }), arguments );
1052 * Adds the elements matched by the expression to the jQuery object. This
1053 * can be used to concatenate the result sets of two expressions.
1055 * @example $("p").add("span")
1056 * @before <p>Hello</p><p><span>Hello Again</span></p>
1057 * @result [ <p>Hello</p>, <span>Hello Again</span> ]
1061 * @param String expr An expression whose matched elements are added
1062 * @cat DOM/Traversing
1066 * Adds each of the Elements in the array to the set of matched elements.
1067 * This is used to add a set of Elements to a jQuery object.
1069 * @example $("p").add([document.getElementById("a"), document.getElementById("b")])
1070 * @before <p>Hello</p><p><span id="a">Hello Again</span><span id="b">And Again</span></p>
1071 * @result [ <p>Hello</p>, <span id="a">Hello Again</span>, <span id="b">And Again</span> ]
1075 * @param Array<Element> els An array of Elements to add
1076 * @cat DOM/Traversing
1080 * Adds a single Element to the set of matched elements. This is used to
1081 * add a single Element to a jQuery object.
1083 * @example $("p").add( document.getElementById("a") )
1084 * @before <p>Hello</p><p><span id="a">Hello Again</span></p>
1085 * @result [ <p>Hello</p>, <span id="a">Hello Again</span> ]
1089 * @param Element el An Element to add
1090 * @cat DOM/Traversing
1093 return this.pushStack( jQuery.merge( this, t.constructor == String ?
1094 jQuery.find(t) : t.constructor == Array ? t : [t] ), arguments );
1098 * Checks the current selection against an expression and returns true,
1099 * if the selection fits the given expression. Does return false, if the
1100 * selection does not fit or the expression is not valid.
1102 * @example $("input[@type='checkbox']").parent().is("form")
1103 * @before <form><input type="checkbox" /></form>
1105 * @desc Returns true, because the parent of the input is a form element
1107 * @example $("input[@type='checkbox']").parent().is("form")
1108 * @before <form><p><input type="checkbox" /></p></form>
1110 * @desc Returns false, because the parent of the input is a p element
1112 * @example $("form").is(null)
1113 * @before <form></form>
1115 * @desc An invalid expression always returns false.
1117 * @test ok( $('#form').is('form'), 'Check for element: A form must be a form' );
1118 * @test ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
1119 * @test ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
1120 * @test ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
1121 * @test ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
1122 * @test ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
1123 * @test ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
1124 * @test ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
1125 * @test ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
1126 * @test ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
1127 * @test ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
1128 * @test ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
1129 * @test ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
1130 * @test ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
1131 * @test ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
1132 * @test ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
1133 * @test ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
1134 * @test ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
1135 * @test ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
1136 * @test ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
1137 * @test ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
1138 * @test ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
1142 * @param String expr The expression with which to filter
1143 * @cat DOM/Traversing
1145 is: function(expr) {
1146 return expr ? jQuery.filter(expr,this).r.length > 0 : false;
1155 * @param Boolean table
1157 * @param Function fn The function doing the DOM manipulation.
1161 domManip: function(args, table, dir, fn){
1162 var clone = this.size() > 1;
1163 var a = jQuery.clean(args);
1165 return this.each(function(){
1168 if ( table && this.nodeName == "TABLE" && a[0].nodeName != "THEAD" ) {
1169 var tbody = this.getElementsByTagName("tbody");
1171 if ( !tbody.length ) {
1172 obj = document.createElement("tbody");
1173 this.appendChild( obj );
1178 for ( var i = ( dir < 0 ? a.length - 1 : 0 );
1179 i != ( dir < 0 ? dir : a.length ); i += dir ) {
1180 fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );
1195 pushStack: function(a,args) {
1196 var fn = args && args[args.length-1];
1198 if ( !fn || fn.constructor != Function ) {
1199 if ( !this.stack ) this.stack = [];
1200 this.stack.push( this.get() );
1203 var old = this.get();
1205 if ( fn.constructor == Function )
1225 * Extend one object with another, returning the original,
1226 * modified, object. This is a great utility for simple inheritance.
1228 * @example var settings = { validate: false, limit: 5, name: "foo" };
1229 * var options = { validate: true, name: "bar" };
1230 * jQuery.extend(settings, options);
1231 * @result settings == { validate: true, limit: 5, name: "bar" }
1233 * @test var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
1234 * var options = { xnumber2: 1, xstring2: "x", xxx: "newstring" };
1235 * var optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" };
1236 * var merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
1237 * jQuery.extend(settings, options);
1238 * isSet( settings, merged, "Check if extended: settings must be extended" );
1239 * isSet ( options, optionsCopy, "Check if not modified: options must not be modified" );
1242 * @param Object obj The object to extend
1243 * @param Object prop The object that will be merged into the first.
1247 jQuery.extend = jQuery.fn.extend = function(obj,prop) {
1248 if ( !prop ) { prop = obj; obj = this; }
1249 for ( var i in prop ) obj[i] = prop[i];
1261 jQuery.initDone = true;
1263 jQuery.each( jQuery.macros.axis, function(i,n){
1264 jQuery.fn[ i ] = function(a) {
1265 var ret = jQuery.map(this,n);
1266 if ( a && a.constructor == String )
1267 ret = jQuery.filter(a,ret).r;
1268 return this.pushStack( ret, arguments );
1272 jQuery.each( jQuery.macros.to, function(i,n){
1273 jQuery.fn[ i ] = function(){
1275 return this.each(function(){
1276 for ( var j = 0; j < a.length; j++ )
1277 jQuery(a[j])[n]( this );
1282 jQuery.each( jQuery.macros.each, function(i,n){
1283 jQuery.fn[ i ] = function() {
1284 return this.each( n, arguments );
1288 jQuery.each( jQuery.macros.filter, function(i,n){
1289 jQuery.fn[ n ] = function(num,fn) {
1290 return this.filter( ":" + n + "(" + num + ")", fn );
1294 jQuery.each( jQuery.macros.attr, function(i,n){
1296 jQuery.fn[ i ] = function(h) {
1297 return h == undefined ?
1298 this.length ? this[0][n] : null :
1303 jQuery.each( jQuery.macros.css, function(i,n){
1304 jQuery.fn[ n ] = function(h) {
1305 return h == undefined ?
1306 ( this.length ? jQuery.css( this[0], n ) : null ) :
1314 * A generic iterator function, which can be used to seemlessly
1315 * iterate over both objects and arrays. This function is not the same
1316 * as $().each() - which is used to iterate, exclusively, over a jQuery
1317 * object. This function can be used to iterate over anything.
1319 * @example $.each( [0,1,2], function(i){
1320 * alert( "Item #" + i + ": " + this );
1322 * @desc This is an example of iterating over the items in an array, accessing both the current item and its index.
1324 * @example $.each( { name: "John", lang: "JS" }, function(i){
1325 * alert( "Name: " + i + ", Value: " + this );
1327 * @desc This is an example of iterating over the properties in an Object, accessing both the current item and its key.
1330 * @param Object obj The object, or array, to iterate over.
1331 * @param Function fn The function that will be executed on every object.
1335 each: function( obj, fn, args ) {
1336 if ( obj.length == undefined )
1337 for ( var i in obj )
1338 fn.apply( obj[i], args || [i, obj[i]] );
1340 for ( var i = 0; i < obj.length; i++ )
1341 fn.apply( obj[i], args || [i, obj[i]] );
1347 if (jQuery.className.has(o,c)) return;
1348 o.className += ( o.className ? " " : "" ) + c;
1350 remove: function(o,c){
1351 o.className = !c ? "" :
1352 o.className.replace(
1353 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
1355 has: function(e,a) {
1356 if ( e.className != undefined )
1358 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
1363 * Swap in/out style options.
1366 swap: function(e,o,f) {
1367 for ( var i in o ) {
1368 e.style["old"+i] = e.style[i];
1373 e.style[i] = e.style["old"+i];
1376 css: function(e,p) {
1377 if ( p == "height" || p == "width" ) {
1378 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
1380 for ( var i in d ) {
1381 old["padding" + d[i]] = 0;
1382 old["border" + d[i] + "Width"] = 0;
1385 jQuery.swap( e, old, function() {
1386 if (jQuery.css(e,"display") != "none") {
1387 oHeight = e.offsetHeight;
1388 oWidth = e.offsetWidth;
1390 e = jQuery(e.cloneNode(true)).css({
1391 visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
1392 }).appendTo(e.parentNode)[0];
1394 var parPos = jQuery.css(e.parentNode,"position");
1395 if ( parPos == "" || parPos == "static" )
1396 e.parentNode.style.position = "relative";
1398 oHeight = e.clientHeight;
1399 oWidth = e.clientWidth;
1401 if ( parPos == "" || parPos == "static" )
1402 e.parentNode.style.position = "static";
1404 e.parentNode.removeChild(e);
1408 return p == "height" ? oHeight : oWidth;
1409 } else if ( p == "opacity" && jQuery.browser.msie )
1410 return parseFloat( jQuery.curCSS(e,"filter").replace(/[^0-9.]/,"") ) || 1;
1412 return jQuery.curCSS( e, p );
1415 curCSS: function(elem, prop, force) {
1418 if (!force && elem.style[prop]) {
1420 ret = elem.style[prop];
1422 } else if (elem.currentStyle) {
1424 var newProp = prop.replace(/\-(\w)/g,function(m,c){return c.toUpperCase();});
1425 ret = elem.currentStyle[prop] || elem.currentStyle[newProp];
1427 } else if (document.defaultView && document.defaultView.getComputedStyle) {
1429 prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();
1430 var cur = document.defaultView.getComputedStyle(elem, null);
1433 ret = cur.getPropertyValue(prop);
1434 else if ( prop == 'display' )
1437 jQuery.swap(elem, { display: 'block' }, function() {
1438 ret = document.defaultView.getComputedStyle(this,null).getPropertyValue(prop);
1446 clean: function(a) {
1448 for ( var i = 0; i < a.length; i++ ) {
1449 if ( a[i].constructor == String ) {
1453 if ( !a[i].indexOf("<thead") || !a[i].indexOf("<tbody") ) {
1455 a[i] = "<table>" + a[i] + "</table>";
1456 } else if ( !a[i].indexOf("<tr") ) {
1458 a[i] = "<table>" + a[i] + "</table>";
1459 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
1461 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
1464 var div = document.createElement("div");
1465 div.innerHTML = a[i];
1468 div = div.firstChild;
1469 if ( table != "thead" ) div = div.firstChild;
1470 if ( table == "td" ) div = div.firstChild;
1473 for ( var j = 0; j < div.childNodes.length; j++ )
1474 r.push( div.childNodes[j] );
1475 } else if ( a[i].jquery || a[i].length && !a[i].nodeType )
1476 for ( var k = 0; k < a[i].length; k++ )
1478 else if ( a[i] !== null )
1479 r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) );
1485 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
1486 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
1494 last: "i==r.length-1",
1499 "nth-child": "jQuery.sibling(a,m[3]).cur",
1500 "first-child": "jQuery.sibling(a,0).cur",
1501 "last-child": "jQuery.sibling(a,0).last",
1502 "only-child": "jQuery.sibling(a).length==1",
1505 parent: "a.childNodes.length",
1506 empty: "!a.childNodes.length",
1509 contains: "(a.innerText||a.innerHTML).indexOf(m[3])>=0",
1512 visible: "a.type!='hidden'&&jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!='hidden'",
1513 hidden: "a.type=='hidden'||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')=='hidden'",
1516 enabled: "!a.disabled",
1517 disabled: "a.disabled",
1518 checked: "a.checked",
1519 selected: "a.selected"
1521 ".": "jQuery.className.has(a,m[2])",
1525 "^=": "!z.indexOf(m[4])",
1526 "$=": "z.substr(z.length - m[4].length,m[4].length)==m[4]",
1527 "*=": "z.indexOf(m[4])>=0",
1530 "[": "jQuery.find(m[2],a).length"
1534 "\\.\\.|/\\.\\.", "a.parentNode",
1535 ">|/", "jQuery.sibling(a.firstChild)",
1536 "\\+", "jQuery.sibling(a).next",
1539 var s = jQuery.sibling(a);
1541 for ( var i = s.n; i < s.length; i++ )
1549 * @test t( "Element Selector", "div", ["main","foo"] );
1550 * @test t( "Element Selector", "body", ["body"] );
1551 * @test t( "Element Selector", "html", ["html"] );
1552 * @test cmpOK( $("*").size(), ">=", 30, "Element Selector" );
1553 * @test t( "Parent Element", "div div", ["foo"] );
1555 * @test t( "ID Selector", "#body", ["body"] );
1556 * @test t( "ID Selector w/ Element", "body#body", ["body"] );
1557 * @test t( "ID Selector w/ Element", "ul#first", [] );
1559 * @test t( "Class Selector", ".blog", ["mark","simon"] );
1560 * @test t( "Class Selector", ".blog.link", ["simon"] );
1561 * @test t( "Class Selector w/ Element", "a.blog", ["mark","simon"] );
1562 * @test t( "Parent Class Selector", "p .blog", ["mark","simon"] );
1564 * @test t( "Comma Support", "a.blog, div", ["mark","simon","main","foo"] );
1565 * @test t( "Comma Support", "a.blog , div", ["mark","simon","main","foo"] );
1566 * @test t( "Comma Support", "a.blog ,div", ["mark","simon","main","foo"] );
1567 * @test t( "Comma Support", "a.blog,div", ["mark","simon","main","foo"] );
1569 * @test t( "Child", "p > a", ["simon1","google","groups","mark","yahoo","simon"] );
1570 * @test t( "Child", "p> a", ["simon1","google","groups","mark","yahoo","simon"] );
1571 * @test t( "Child", "p >a", ["simon1","google","groups","mark","yahoo","simon"] );
1572 * @test t( "Child", "p>a", ["simon1","google","groups","mark","yahoo","simon"] );
1573 * @test t( "Child w/ Class", "p > a.blog", ["mark","simon"] );
1574 * @test t( "All Children", "code > *", ["anchor1","anchor2"] );
1575 * @test t( "All Grandchildren", "p > * > *", ["anchor1","anchor2"] );
1576 * @test t( "Adjacent", "a + a", ["groups"] );
1577 * @test t( "Adjacent", "a +a", ["groups"] );
1578 * @test t( "Adjacent", "a+ a", ["groups"] );
1579 * @test t( "Adjacent", "a+a", ["groups"] );
1580 * @test t( "Adjacent", "p + p", ["ap","en","sap"] );
1581 * @test t( "Comma, Child, and Adjacent", "a + a, code > a", ["groups","anchor1","anchor2"] );
1582 * @test t( "First Child", "p:first-child", ["firstp","sndp"] );
1583 * @test t( "Attribute Exists", "a[@title]", ["google"] );
1584 * @test t( "Attribute Exists", "*[@title]", ["google"] );
1585 * @test t( "Attribute Exists", "[@title]", ["google"] );
1586 * @test t( "Attribute Equals", "a[@rel='bookmark']", ["simon1"] );
1587 * @test t( "Attribute Equals", 'a[@rel="bookmark"]', ["simon1"] );
1588 * @test t( "Attribute Equals", "a[@rel=bookmark]", ["simon1"] );
1589 * @test t( "Multiple Attribute Equals", "input[@type='hidden'],input[@type='radio']", ["hidden1","radio1","radio2"] );
1590 * @test t( "Multiple Attribute Equals", "input[@type=\"hidden\"],input[@type='radio']", ["hidden1","radio1","radio2"] );
1591 * @test t( "Multiple Attribute Equals", "input[@type=hidden],input[@type=radio]", ["hidden1","radio1","radio2"] );
1593 * @test t( "Attribute Begins With", "a[@href ^= 'http://www']", ["google","yahoo"] );
1594 * @test t( "Attribute Ends With", "a[@href $= 'org/']", ["mark"] );
1595 * @test t( "Attribute Contains", "a[@href *= 'google']", ["google","groups"] );
1596 * @test t( "First Child", "p:first-child", ["firstp","sndp"] );
1597 * @test t( "Last Child", "p:last-child", ["sap"] );
1598 * @test t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] );
1599 * @test t( "Empty", "ul:empty", ["firstUL"] );
1600 * @test t( "Enabled UI Element", "input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2","name"] );
1601 * @test t( "Disabled UI Element", "input:disabled", ["text2"] );
1602 * @test t( "Checked UI Element", "input:checked", ["radio2","check1"] );
1603 * @test t( "Selected Option Element", "option:selected", ["option1a","option2d","option3b","option3c"] );
1604 * @test t( "Text Contains", "a:contains('Google')", ["google","groups"] );
1605 * @test t( "Text Contains", "a:contains('Google Groups')", ["groups"] );
1606 * @test t( "Element Preceded By", "p ~ div", ["foo"] );
1607 * @test t( "Not", "a.blog:not(.link)", ["mark"] );
1609 * @test cmpOK( jQuery.find("//*").length, ">=", 30, "All Elements (//*)" );
1610 * @test t( "All Div Elements", "//div", ["main","foo"] );
1611 * @test t( "Absolute Path", "/html/body", ["body"] );
1612 * @test t( "Absolute Path w/ *", "/* /body", ["body"] );
1613 * @test t( "Long Absolute Path", "/html/body/dl/div/div/p", ["sndp","en","sap"] );
1614 * @test t( "Absolute and Relative Paths", "/html//div", ["main","foo"] );
1615 * @test t( "All Children, Explicit", "//code/*", ["anchor1","anchor2"] );
1616 * @test t( "All Children, Implicit", "//code/", ["anchor1","anchor2"] );
1617 * @test t( "Attribute Exists", "//a[@title]", ["google"] );
1618 * @test t( "Attribute Equals", "//a[@rel='bookmark']", ["simon1"] );
1619 * @test t( "Parent Axis", "//p/..", ["main","foo"] );
1620 * @test t( "Sibling Axis", "//p/../", ["firstp","ap","foo","first","firstUL","empty","form","sndp","en","sap"] );
1621 * @test t( "Sibling Axis", "//p/../*", ["firstp","ap","foo","first","firstUL","empty","form","sndp","en","sap"] );
1622 * @test t( "Has Children", "//p[a]", ["firstp","ap","en","sap"] );
1624 * @test t( "nth Element", "p:nth(1)", ["ap"] );
1625 * @test t( "First Element", "p:first", ["firstp"] );
1626 * @test t( "Last Element", "p:last", ["first"] );
1627 * @test t( "Even Elements", "p:even", ["firstp","sndp","sap"] );
1628 * @test t( "Odd Elements", "p:odd", ["ap","en","first"] );
1629 * @test t( "Position Equals", "p:eq(1)", ["ap"] );
1630 * @test t( "Position Greater Than", "p:gt(0)", ["ap","sndp","en","sap","first"] );
1631 * @test t( "Position Less Than", "p:lt(3)", ["firstp","ap","sndp"] );
1632 * @test t( "Is A Parent", "p:parent", ["firstp","ap","sndp","en","sap","first"] );
1633 * @test t( "Is Visible", "input:visible", ["text1","text2","radio1","radio2","check1","check2","name"] );
1634 * @test t( "Is Hidden", "input:hidden", ["hidden1","hidden2"] );
1636 * @test t( "Grouped Form Elements", "input[@name='foo[bar]']", ["hidden2"] );
1638 * @test t( "All Children of ID", "#foo/*", ["sndp", "en", "sap"] );
1639 * @test t( "All Children of ID with no children", "#firstUL/*", [] );
1642 * @type Array<Element>
1646 find: function( t, context ) {
1647 // Make sure that the context is a DOM Element
1648 if ( context && context.nodeType == undefined )
1651 // Set the correct context (if none is provided)
1652 context = context || jQuery.context || document;
1654 if ( t.constructor != String ) return [t];
1656 if ( !t.indexOf("//") ) {
1657 context = context.documentElement;
1658 t = t.substr(2,t.length);
1659 } else if ( !t.indexOf("/") ) {
1660 context = context.documentElement;
1661 t = t.substr(1,t.length);
1662 // FIX Assume the root element is right :(
1663 if ( t.indexOf("/") >= 1 )
1664 t = t.substr(t.indexOf("/"),t.length);
1667 var ret = [context];
1671 while ( t.length > 0 && last != t ) {
1675 t = jQuery.trim(t).replace( /^\/\//i, "" );
1677 var foundToken = false;
1679 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
1680 if ( foundToken ) continue;
1682 var re = new RegExp("^(" + jQuery.token[i] + ")");
1686 r = ret = jQuery.map( ret, jQuery.token[i+1] );
1687 t = jQuery.trim( t.replace( re, "" ) );
1692 if ( !foundToken ) {
1693 if ( !t.indexOf(",") || !t.indexOf("|") ) {
1694 if ( ret[0] == context ) ret.shift();
1695 done = jQuery.merge( done, ret );
1696 r = ret = [context];
1697 t = " " + t.substr(1,t.length);
1699 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
1700 var m = re2.exec(t);
1702 if ( m[1] == "#" ) {
1703 // Ummm, should make this work in all XML docs
1704 var oid = document.getElementById(m[2]);
1705 r = ret = oid ? [oid] : [];
1706 t = t.replace( re2, "" );
1708 if ( !m[2] || m[1] == "." ) m[2] = "*";
1710 for ( var i = 0; i < ret.length; i++ )
1711 r = jQuery.merge( r,
1713 jQuery.getAll(ret[i]) :
1714 ret[i].getElementsByTagName(m[2])
1722 var val = jQuery.filter(t,r);
1724 t = jQuery.trim(val.t);
1728 if ( ret && ret[0] == context ) ret.shift();
1729 done = jQuery.merge( done, ret );
1734 getAll: function(o,r) {
1736 var s = o.childNodes;
1737 for ( var i = 0; i < s.length; i++ )
1738 if ( s[i].nodeType == 1 ) {
1740 jQuery.getAll( s[i], r );
1745 attr: function(elem, name, value){
1748 "class": "className",
1749 "float": "cssFloat",
1750 innerHTML: "innerHTML",
1751 className: "className",
1753 disabled: "disabled",
1758 if ( value != undefined ) elem[fix[name]] = value;
1759 return elem[fix[name]];
1760 } else if ( elem.getAttribute != undefined ) {
1761 if ( value != undefined ) elem.setAttribute( name, value );
1762 return elem.getAttribute( name, 2 );
1764 name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();});
1765 if ( value != undefined ) elem[name] = value;
1770 // The regular expressions that power the parsing engine
1772 // Match: [@value='test'], [@foo]
1773 [ "\\[ *(@)S *([!*$^=]*) *Q\\]", 1 ],
1775 // Match: [div], [div p]
1778 // Match: :contains('foo')
1779 [ "(:)S\\(Q\\)", 0 ],
1781 // Match: :even, :last-chlid
1785 filter: function(t,r,not) {
1786 // Figure out if we're doing regular, or inverse, filtering
1787 var g = not !== false ? jQuery.grep :
1788 function(a,f) {return jQuery.grep(a,f,true);};
1790 while ( t && /^[a-z[({<*:.#]/i.test(t) ) {
1792 var p = jQuery.parse;
1794 for ( var i = 0; i < p.length; i++ ) {
1795 var re = new RegExp( "^" + p[i][0]
1797 // Look for a string-like sequence
1798 .replace( 'S', "([a-z*_-][a-z0-9_-]*)" )
1800 // Look for something (optionally) enclosed with quotes
1801 .replace( 'Q', " *'?\"?([^'\"]*?)'?\"? *" ), "i" );
1803 var m = re.exec( t );
1806 // Re-organize the match
1808 m = ["", m[1], m[3], m[2], m[4]];
1810 // Remove what we just matched
1811 t = t.replace( re, "" );
1817 // :not() is a special case that can be optomized by
1818 // keeping it out of the expression list
1819 if ( m[1] == ":" && m[2] == "not" )
1820 r = jQuery.filter(m[3],r,false).r;
1822 // Otherwise, find the expression to execute
1824 var f = jQuery.expr[m[1]];
1825 if ( f.constructor != String )
1826 f = jQuery.expr[m[1]][m[2]];
1828 // Build a custom macro to enclose it
1829 eval("f = function(a,i){" +
1830 ( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) +
1831 "return " + f + "}");
1833 // Execute it against the current filter
1838 // Return an array of filtered elements (r)
1839 // and the modified expression string (t)
1840 return { r: r, t: t };
1844 * Remove the whitespace from the beginning and end of a string.
1846 * @example $.trim(" hello, how are you? ");
1847 * @result "hello, how are you?"
1851 * @param String str The string to trim.
1855 return t.replace(/^\s+|\s+$/g, "");
1859 * All ancestors of a given element.
1863 * @type Array<Element>
1864 * @param Element elem The element to find the ancestors of.
1865 * @cat DOM/Traversing
1867 parents: function( elem ){
1869 var cur = elem.parentNode;
1870 while ( cur && cur != document ) {
1871 matched.push( cur );
1872 cur = cur.parentNode;
1878 * All elements on a specified axis.
1883 * @param Element elem The element to find all the siblings of (including itself).
1884 * @cat DOM/Traversing
1886 sibling: function(elem, pos, not) {
1889 var siblings = elem.parentNode.childNodes;
1890 for ( var i = 0; i < siblings.length; i++ ) {
1891 if ( not === true && siblings[i] == elem ) continue;
1893 if ( siblings[i].nodeType == 1 )
1894 elems.push( siblings[i] );
1895 if ( siblings[i] == elem )
1896 elems.n = elems.length - 1;
1899 return jQuery.extend( elems, {
1900 last: elems.n == elems.length - 1,
1901 cur: pos == "even" && elems.n % 2 == 0 || pos == "odd" && elems.n % 2 || elems[pos] == elem,
1902 prev: elems[elems.n - 1],
1903 next: elems[elems.n + 1]
1908 * Merge two arrays together, removing all duplicates. The final order
1909 * or the new array is: All the results from the first array, followed
1910 * by the unique results from the second array.
1912 * @example $.merge( [0,1,2], [2,3,4] )
1913 * @result [0,1,2,3,4]
1915 * @example $.merge( [3,2,1], [4,3,2] )
1920 * @param Array first The first array to merge.
1921 * @param Array second The second array to merge.
1924 merge: function(first, second) {
1927 // Move b over to the new array (this helps to avoid
1928 // StaticNodeList instances)
1929 for ( var k = 0; k < first.length; k++ )
1930 result[k] = first[k];
1932 // Now check for duplicates between a and b and only
1933 // add the unique items
1934 for ( var i = 0; i < second.length; i++ ) {
1935 var noCollision = true;
1937 // The collision-checking process
1938 for ( var j = 0; j < first.length; j++ )
1939 if ( second[i] == first[j] )
1940 noCollision = false;
1942 // If the item is unique, add it
1944 result.push( second[i] );
1951 * Filter items out of an array, by using a filter function.
1952 * The specified function will be passed two arguments: The
1953 * current array item and the index of the item in the array. The
1954 * function should return 'true' if you wish to keep the item in
1955 * the array, false if it should be removed.
1957 * @example $.grep( [0,1,2], function(i){
1964 * @param Array array The Array to find items in.
1965 * @param Function fn The function to process each item against.
1966 * @param Boolean inv Invert the selection - select the opposite of the function.
1969 grep: function(elems, fn, inv) {
1970 // If a string is passed in for the function, make a function
1971 // for it (a handy shortcut)
1972 if ( fn.constructor == String )
1973 fn = new Function("a","i","return " + fn);
1977 // Go through the array, only saving the items
1978 // that pass the validator function
1979 for ( var i = 0; i < elems.length; i++ )
1980 if ( !inv && fn(elems[i],i) || inv && !fn(elems[i],i) )
1981 result.push( elems[i] );
1987 * Translate all items in an array to another array of items.
1988 * The translation function that is provided to this method is
1989 * called for each item in the array and is passed one argument:
1990 * The item to be translated. The function can then return:
1991 * The translated value, 'null' (to remove the item), or
1992 * an array of values - which will be flattened into the full array.
1994 * @example $.map( [0,1,2], function(i){
1999 * @example $.map( [0,1,2], function(i){
2000 * return i > 0 ? i + 1 : null;
2004 * @example $.map( [0,1,2], function(i){
2005 * return [ i, i + 1 ];
2007 * @result [0, 1, 1, 2, 2, 3]
2011 * @param Array array The Array to translate.
2012 * @param Function fn The function to process each item against.
2015 map: function(elems, fn) {
2016 // If a string is passed in for the function, make a function
2017 // for it (a handy shortcut)
2018 if ( fn.constructor == String )
2019 fn = new Function("a","return " + fn);
2023 // Go through the array, translating each of the items to their
2024 // new value (or values).
2025 for ( var i = 0; i < elems.length; i++ ) {
2026 var val = fn(elems[i],i);
2028 if ( val !== null && val != undefined ) {
2029 if ( val.constructor != Array ) val = [val];
2030 result = jQuery.merge( result, val );
2038 * A number of helper functions used for managing events.
2039 * Many of the ideas behind this code orignated from Dean Edwards' addEvent library.
2043 // Bind an event to an element
2044 // Original by Dean Edwards
2045 add: function(element, type, handler) {
2046 // For whatever reason, IE has trouble passing the window object
2047 // around, causing it to be cloned in the process
2048 if ( jQuery.browser.msie && element.setInterval != undefined )
2051 // Make sure that the function being executed has a unique ID
2052 if ( !handler.guid )
2053 handler.guid = this.guid++;
2055 // Init the element's event structure
2056 if (!element.events)
2057 element.events = {};
2059 // Get the current list of functions bound to this event
2060 var handlers = element.events[type];
2062 // If it hasn't been initialized yet
2064 // Init the event handler queue
2065 handlers = element.events[type] = {};
2067 // Remember an existing handler, if it's already there
2068 if (element["on" + type])
2069 handlers[0] = element["on" + type];
2072 // Add the function to the element's handler list
2073 handlers[handler.guid] = handler;
2075 // And bind the global event handler to the element
2076 element["on" + type] = this.handle;
2078 // Remember the function in a global list (for triggering)
2079 if (!this.global[type])
2080 this.global[type] = [];
2081 this.global[type].push( element );
2087 // Detach an event or set of events from an element
2088 remove: function(element, type, handler) {
2090 if (type && element.events[type])
2092 delete element.events[type][handler.guid];
2094 for ( var i in element.events[type] )
2095 delete element.events[type][i];
2097 for ( var j in element.events )
2098 this.remove( element, j );
2101 trigger: function(type,data,element) {
2102 // Touch up the incoming data
2105 // Handle a global trigger
2107 var g = this.global[type];
2109 for ( var i = 0; i < g.length; i++ )
2110 this.trigger( type, data, g[i] );
2112 // Handle triggering a single element
2113 } else if ( element["on" + type] ) {
2114 // Pass along a fake event
2115 data.unshift( this.fix({ type: type, target: element }) );
2117 // Trigger the event
2118 element["on" + type].apply( element, data );
2122 handle: function(event) {
2123 if ( typeof jQuery == "undefined" ) return;
2125 event = event || jQuery.event.fix( window.event );
2127 // If no correct event was found, fail
2128 if ( !event ) return;
2130 var returnValue = true;
2132 var c = this.events[event.type];
2134 var args = [].slice.call( arguments, 1 );
2135 args.unshiftT( event );
2137 for ( var j in c ) {
2138 if ( c[j].apply( this, args ) === false ) {
2139 event.preventDefault();
2140 event.stopPropagation();
2141 returnValue = false;
2148 fix: function(event) {
2150 event.preventDefault = function() {
2151 this.returnValue = false;
2154 event.stopPropagation = function() {
2155 this.cancelBubble = true;
2166 var b = navigator.userAgent.toLowerCase();
2168 // Figure out what browser is being used
2170 safari: /webkit/.test(b),
2171 opera: /opera/.test(b),
2172 msie: /msie/.test(b) && !/opera/.test(b),
2173 mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
2176 // Check to see if the W3C box model is being used
2177 jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
2183 * Append all of the matched elements to another, specified, set of elements.
2184 * This operation is, essentially, the reverse of doing a regular
2185 * $(A).append(B), in that instead of appending B to A, you're appending
2188 * @example $("p").appendTo("#foo");
2189 * @before <p>I would like to say: </p><div id="foo"></div>
2190 * @result <div id="foo"><p>I would like to say: </p></div>
2194 * @param String expr A jQuery expression of elements to match.
2195 * @cat DOM/Manipulation
2200 * Prepend all of the matched elements to another, specified, set of elements.
2201 * This operation is, essentially, the reverse of doing a regular
2202 * $(A).prepend(B), in that instead of prepending B to A, you're prepending
2205 * @example $("p").prependTo("#foo");
2206 * @before <p>I would like to say: </p><div id="foo"><b>Hello</b></div>
2207 * @result <div id="foo"><p>I would like to say: </p><b>Hello</b></div>
2211 * @param String expr A jQuery expression of elements to match.
2212 * @cat DOM/Manipulation
2214 prependTo: "prepend",
2217 * Insert all of the matched elements before another, specified, set of elements.
2218 * This operation is, essentially, the reverse of doing a regular
2219 * $(A).before(B), in that instead of inserting B before A, you're inserting
2222 * @example $("p").insertBefore("#foo");
2223 * @before <div id="foo">Hello</div><p>I would like to say: </p>
2224 * @result <p>I would like to say: </p><div id="foo">Hello</div>
2226 * @name insertBefore
2228 * @param String expr A jQuery expression of elements to match.
2229 * @cat DOM/Manipulation
2231 insertBefore: "before",
2234 * Insert all of the matched elements after another, specified, set of elements.
2235 * This operation is, essentially, the reverse of doing a regular
2236 * $(A).after(B), in that instead of inserting B after A, you're inserting
2239 * @example $("p").insertAfter("#foo");
2240 * @before <p>I would like to say: </p><div id="foo">Hello</div>
2241 * @result <div id="foo">Hello</div><p>I would like to say: </p>
2245 * @param String expr A jQuery expression of elements to match.
2246 * @cat DOM/Manipulation
2248 insertAfter: "after"
2252 * Get the current CSS width of the first matched element.
2254 * @example $("p").width();
2255 * @before <p>This is just a test.</p>
2264 * Set the CSS width of every matched element. Be sure to include
2265 * the "px" (or other unit of measurement) after the number that you
2266 * specify, otherwise you might get strange results.
2268 * @example $("p").width("20px");
2269 * @before <p>This is just a test.</p>
2270 * @result <p style="width:20px;">This is just a test.</p>
2274 * @param String val Set the CSS property to the specified value.
2279 * Get the current CSS height of the first matched element.
2281 * @example $("p").height();
2282 * @before <p>This is just a test.</p>
2291 * Set the CSS height of every matched element. Be sure to include
2292 * the "px" (or other unit of measurement) after the number that you
2293 * specify, otherwise you might get strange results.
2295 * @example $("p").height("20px");
2296 * @before <p>This is just a test.</p>
2297 * @result <p style="height:20px;">This is just a test.</p>
2301 * @param String val Set the CSS property to the specified value.
2306 * Get the current CSS top of the first matched element.
2308 * @example $("p").top();
2309 * @before <p>This is just a test.</p>
2318 * Set the CSS top of every matched element. Be sure to include
2319 * the "px" (or other unit of measurement) after the number that you
2320 * specify, otherwise you might get strange results.
2322 * @example $("p").top("20px");
2323 * @before <p>This is just a test.</p>
2324 * @result <p style="top:20px;">This is just a test.</p>
2328 * @param String val Set the CSS property to the specified value.
2333 * Get the current CSS left of the first matched element.
2335 * @example $("p").left();
2336 * @before <p>This is just a test.</p>
2345 * Set the CSS left of every matched element. Be sure to include
2346 * the "px" (or other unit of measurement) after the number that you
2347 * specify, otherwise you might get strange results.
2349 * @example $("p").left("20px");
2350 * @before <p>This is just a test.</p>
2351 * @result <p style="left:20px;">This is just a test.</p>
2355 * @param String val Set the CSS property to the specified value.
2360 * Get the current CSS position of the first matched element.
2362 * @example $("p").position();
2363 * @before <p>This is just a test.</p>
2372 * Set the CSS position of every matched element.
2374 * @example $("p").position("relative");
2375 * @before <p>This is just a test.</p>
2376 * @result <p style="position:relative;">This is just a test.</p>
2380 * @param String val Set the CSS property to the specified value.
2385 * Get the current CSS float of the first matched element.
2387 * @example $("p").float();
2388 * @before <p>This is just a test.</p>
2397 * Set the CSS float of every matched element.
2399 * @example $("p").float("left");
2400 * @before <p>This is just a test.</p>
2401 * @result <p style="float:left;">This is just a test.</p>
2405 * @param String val Set the CSS property to the specified value.
2410 * Get the current CSS overflow of the first matched element.
2412 * @example $("p").overflow();
2413 * @before <p>This is just a test.</p>
2422 * Set the CSS overflow of every matched element.
2424 * @example $("p").overflow("auto");
2425 * @before <p>This is just a test.</p>
2426 * @result <p style="overflow:auto;">This is just a test.</p>
2430 * @param String val Set the CSS property to the specified value.
2435 * Get the current CSS color of the first matched element.
2437 * @example $("p").color();
2438 * @before <p>This is just a test.</p>
2447 * Set the CSS color of every matched element.
2449 * @example $("p").color("blue");
2450 * @before <p>This is just a test.</p>
2451 * @result <p style="color:blue;">This is just a test.</p>
2455 * @param String val Set the CSS property to the specified value.
2460 * Get the current CSS background of the first matched element.
2462 * @example $("p").background();
2463 * @before <p style="background:blue;">This is just a test.</p>
2472 * Set the CSS background of every matched element.
2474 * @example $("p").background("blue");
2475 * @before <p>This is just a test.</p>
2476 * @result <p style="background:blue;">This is just a test.</p>
2480 * @param String val Set the CSS property to the specified value.
2484 css: "width,height,top,left,position,float,overflow,color,background".split(","),
2487 * Reduce the set of matched elements to a single element.
2488 * The position of the element in the set of matched elements
2489 * starts at 0 and goes to length - 1.
2491 * @example $("p").eq(1)
2492 * @before <p>This is just a test.</p><p>So is this</p>
2493 * @result [ <p>So is this</p> ]
2497 * @param Number pos The index of the element that you wish to limit to.
2502 * Reduce the set of matched elements to all elements before a given position.
2503 * The position of the element in the set of matched elements
2504 * starts at 0 and goes to length - 1.
2506 * @example $("p").lt(1)
2507 * @before <p>This is just a test.</p><p>So is this</p>
2508 * @result [ <p>This is just a test.</p> ]
2512 * @param Number pos Reduce the set to all elements below this position.
2517 * Reduce the set of matched elements to all elements after a given position.
2518 * The position of the element in the set of matched elements
2519 * starts at 0 and goes to length - 1.
2521 * @example $("p").gt(0)
2522 * @before <p>This is just a test.</p><p>So is this</p>
2523 * @result [ <p>So is this</p> ]
2527 * @param Number pos Reduce the set to all elements after this position.
2532 * Filter the set of elements to those that contain the specified text.
2534 * @example $("p").contains("test")
2535 * @before <p>This is just a test.</p><p>So is this</p>
2536 * @result [ <p>This is just a test.</p> ]
2540 * @param String str The string that will be contained within the text of an element.
2541 * @cat DOM/Traversing
2544 filter: [ "eq", "lt", "gt", "contains" ],
2548 * Get the current value of the first matched element.
2550 * @example $("input").val();
2551 * @before <input type="text" value="some text"/>
2552 * @result "some text"
2554 * @test ok( $("#text1").val() == "Test", "Check for value of input element" );
2555 * @test ok( !$("#text1").val() == "", "Check for value of input element" );
2559 * @cat DOM/Attributes
2563 * Set the value of every matched element.
2565 * @example $("input").value("test");
2566 * @before <input type="text" value="some text"/>
2567 * @result <input type="text" value="test"/>
2569 * @test document.getElementById('text1').value = "bla";
2570 * ok( $("#text1").val() == "bla", "Check for modified value of input element" );
2571 * $("#text1").val('test');
2572 * ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
2576 * @param String val Set the property to the specified value.
2577 * @cat DOM/Attributes
2582 * Get the html contents of the first matched element.
2584 * @example $("div").html();
2585 * @before <div><input/></div>
2590 * @cat DOM/Attributes
2594 * Set the html contents of every matched element.
2596 * @example $("div").html("<b>new stuff</b>");
2597 * @before <div><input/></div>
2598 * @result <div><b>new stuff</b></div>
2600 * @test var div = $("div");
2601 * div.html("<b>test</b>");
2603 * for ( var i = 0; i < div.size(); i++ ) {
2604 * if ( div.get(i).childNodes.length == 0 ) pass = false;
2606 * ok( pass, "Set HTML" );
2610 * @param String val Set the html contents to the specified value.
2611 * @cat DOM/Attributes
2616 * Get the current id of the first matched element.
2618 * @example $("input").id();
2619 * @before <input type="text" id="test" value="some text"/>
2624 * @cat DOM/Attributes
2628 * Set the id of every matched element.
2630 * @example $("input").id("newid");
2631 * @before <input type="text" id="test" value="some text"/>
2632 * @result <input type="text" id="newid" value="some text"/>
2636 * @param String val Set the property to the specified value.
2637 * @cat DOM/Attributes
2642 * Get the current title of the first matched element.
2644 * @example $("img").title();
2645 * @before <img src="test.jpg" title="my image"/>
2646 * @result "my image"
2650 * @cat DOM/Attributes
2654 * Set the title of every matched element.
2656 * @example $("img").title("new title");
2657 * @before <img src="test.jpg" title="my image"/>
2658 * @result <img src="test.jpg" title="new image"/>
2662 * @param String val Set the property to the specified value.
2663 * @cat DOM/Attributes
2668 * Get the current name of the first matched element.
2670 * @example $("input").name();
2671 * @before <input type="text" name="username"/>
2672 * @result "username"
2676 * @cat DOM/Attributes
2680 * Set the name of every matched element.
2682 * @example $("input").name("user");
2683 * @before <input type="text" name="username"/>
2684 * @result <input type="text" name="user"/>
2688 * @param String val Set the property to the specified value.
2689 * @cat DOM/Attributes
2694 * Get the current href of the first matched element.
2696 * @example $("a").href();
2697 * @before <a href="test.html">my link</a>
2698 * @result "test.html"
2702 * @cat DOM/Attributes
2706 * Set the href of every matched element.
2708 * @example $("a").href("test2.html");
2709 * @before <a href="test.html">my link</a>
2710 * @result <a href="test2.html">my link</a>
2714 * @param String val Set the property to the specified value.
2715 * @cat DOM/Attributes
2720 * Get the current src of the first matched element.
2722 * @example $("img").src();
2723 * @before <img src="test.jpg" title="my image"/>
2724 * @result "test.jpg"
2728 * @cat DOM/Attributes
2732 * Set the src of every matched element.
2734 * @example $("img").src("test2.jpg");
2735 * @before <img src="test.jpg" title="my image"/>
2736 * @result <img src="test2.jpg" title="my image"/>
2740 * @param String val Set the property to the specified value.
2741 * @cat DOM/Attributes
2746 * Get the current rel of the first matched element.
2748 * @example $("a").rel();
2749 * @before <a href="test.html" rel="nofollow">my link</a>
2750 * @result "nofollow"
2754 * @cat DOM/Attributes
2758 * Set the rel of every matched element.
2760 * @example $("a").rel("nofollow");
2761 * @before <a href="test.html">my link</a>
2762 * @result <a href="test.html" rel="nofollow">my link</a>
2766 * @param String val Set the property to the specified value.
2767 * @cat DOM/Attributes
2774 * Get a set of elements containing the unique parents of the matched
2777 * @example $("p").parent()
2778 * @before <div><p>Hello</p><p>Hello</p></div>
2779 * @result [ <div><p>Hello</p><p>Hello</p></div> ]
2783 * @cat DOM/Traversing
2787 * Get a set of elements containing the unique parents of the matched
2788 * set of elements, and filtered by an expression.
2790 * @example $("p").parent(".selected")
2791 * @before <div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>
2792 * @result [ <div class="selected"><p>Hello Again</p></div> ]
2796 * @param String expr An expression to filter the parents with
2797 * @cat DOM/Traversing
2799 parent: "a.parentNode",
2802 * Get a set of elements containing the unique ancestors of the matched
2803 * set of elements (except for the root element).
2805 * @example $("span").ancestors()
2806 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
2807 * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
2811 * @cat DOM/Traversing
2815 * Get a set of elements containing the unique ancestors of the matched
2816 * set of elements, and filtered by an expression.
2818 * @example $("span").ancestors("p")
2819 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
2820 * @result [ <p><span>Hello</span></p> ]
2824 * @param String expr An expression to filter the ancestors with
2825 * @cat DOM/Traversing
2827 ancestors: jQuery.parents,
2830 * Get a set of elements containing the unique ancestors of the matched
2831 * set of elements (except for the root element).
2833 * @example $("span").ancestors()
2834 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
2835 * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
2839 * @cat DOM/Traversing
2843 * Get a set of elements containing the unique ancestors of the matched
2844 * set of elements, and filtered by an expression.
2846 * @example $("span").ancestors("p")
2847 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
2848 * @result [ <p><span>Hello</span></p> ]
2852 * @param String expr An expression to filter the ancestors with
2853 * @cat DOM/Traversing
2855 parents: jQuery.parents,
2858 * Get a set of elements containing the unique next siblings of each of the
2859 * matched set of elements.
2861 * It only returns the very next sibling, not all next siblings.
2863 * @example $("p").next()
2864 * @before <p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
2865 * @result [ <p>Hello Again</p>, <div><span>And Again</span></div> ]
2869 * @cat DOM/Traversing
2873 * Get a set of elements containing the unique next siblings of each of the
2874 * matched set of elements, and filtered by an expression.
2876 * It only returns the very next sibling, not all next siblings.
2878 * @example $("p").next(".selected")
2879 * @before <p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>
2880 * @result [ <p class="selected">Hello Again</p> ]
2884 * @param String expr An expression to filter the next Elements with
2885 * @cat DOM/Traversing
2887 next: "jQuery.sibling(a).next",
2890 * Get a set of elements containing the unique previous siblings of each of the
2891 * matched set of elements.
2893 * It only returns the immediately previous sibling, not all previous siblings.
2895 * @example $("p").previous()
2896 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
2897 * @result [ <div><span>Hello Again</span></div> ]
2901 * @cat DOM/Traversing
2905 * Get a set of elements containing the unique previous siblings of each of the
2906 * matched set of elements, and filtered by an expression.
2908 * It only returns the immediately previous sibling, not all previous siblings.
2910 * @example $("p").previous(".selected")
2911 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
2912 * @result [ <div><span>Hello</span></div> ]
2916 * @param String expr An expression to filter the previous Elements with
2917 * @cat DOM/Traversing
2919 prev: "jQuery.sibling(a).prev",
2922 * Get a set of elements containing all of the unique siblings of each of the
2923 * matched set of elements.
2925 * @example $("div").siblings()
2926 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
2927 * @result [ <p>Hello</p>, <p>And Again</p> ]
2929 * @test isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
2933 * @cat DOM/Traversing
2937 * Get a set of elements containing all of the unique siblings of each of the
2938 * matched set of elements, and filtered by an expression.
2940 * @example $("div").siblings(".selected")
2941 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
2942 * @result [ <p class="selected">Hello Again</p> ]
2944 * @test isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" );
2945 * @test isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
2949 * @param String expr An expression to filter the sibling Elements with
2950 * @cat DOM/Traversing
2952 siblings: jQuery.sibling,
2956 * Get a set of elements containing all of the unique children of each of the
2957 * matched set of elements.
2959 * @example $("div").children()
2960 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
2961 * @result [ <span>Hello Again</span> ]
2965 * @cat DOM/Traversing
2969 * Get a set of elements containing all of the unique children of each of the
2970 * matched set of elements, and filtered by an expression.
2972 * @example $("div").children(".selected")
2973 * @before <div><span>Hello</span><p class="selected">Hello Again</p><p>And Again</p></div>
2974 * @result [ <p class="selected">Hello Again</p> ]
2978 * @param String expr An expression to filter the child Elements with
2979 * @cat DOM/Traversing
2981 children: "jQuery.sibling(a.firstChild)"
2987 * Remove an attribute from each of the matched elements.
2989 * @example $("input").removeAttr("disabled")
2990 * @before <input disabled="disabled"/>
2995 * @param String name The name of the attribute to remove.
2998 removeAttr: function( key ) {
2999 this.removeAttribute( key );
3003 * Displays each of the set of matched elements if they are hidden.
3005 * @example $("p").show()
3006 * @before <p style="display: none">Hello</p>
3007 * @result [ <p style="display: block">Hello</p> ]
3009 * @test var pass = true, div = $("div");
3010 * div.show().each(function(){
3011 * if ( this.style.display == "none" ) pass = false;
3013 * ok( pass, "Show" );
3020 this.style.display = this.oldblock ? this.oldblock : "";
3021 if ( jQuery.css(this,"display") == "none" )
3022 this.style.display = "block";
3026 * Hides each of the set of matched elements if they are shown.
3028 * @example $("p").hide()
3029 * @before <p>Hello</p>
3030 * @result [ <p style="display: none">Hello</p> ]
3032 * var pass = true, div = $("div");
3033 * div.hide().each(function(){
3034 * if ( this.style.display != "none" ) pass = false;
3036 * ok( pass, "Hide" );
3043 this.oldblock = this.oldblock || jQuery.css(this,"display");
3044 if ( this.oldblock == "none" )
3045 this.oldblock = "block";
3046 this.style.display = "none";
3050 * Toggles each of the set of matched elements. If they are shown,
3051 * toggle makes them hidden. If they are hidden, toggle
3054 * @example $("p").toggle()
3055 * @before <p>Hello</p><p style="display: none">Hello Again</p>
3056 * @result [ <p style="display: none">Hello</p>, <p style="display: block">Hello Again</p> ]
3063 jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ].apply( jQuery(this), arguments );
3067 * Adds the specified class to each of the set of matched elements.
3069 * @example $("p").addClass("selected")
3070 * @before <p>Hello</p>
3071 * @result [ <p class="selected">Hello</p> ]
3073 * @test var div = $("div");
3074 * div.addClass("test");
3076 * for ( var i = 0; i < div.size(); i++ ) {
3077 * if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
3079 * ok( pass, "Add Class" );
3083 * @param String class A CSS class to add to the elements
3086 addClass: function(c){
3087 jQuery.className.add(this,c);
3091 * Removes the specified class from the set of matched elements.
3093 * @example $("p").removeClass("selected")
3094 * @before <p class="selected">Hello</p>
3095 * @result [ <p>Hello</p> ]
3097 * @test var div = $("div").addClass("test");
3098 * div.removeClass("test");
3100 * for ( var i = 0; i < div.size(); i++ ) {
3101 * if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
3103 * ok( pass, "Remove Class" );
3107 * @param String class A CSS class to remove from the elements
3110 removeClass: function(c){
3111 jQuery.className.remove(this,c);
3115 * Adds the specified class if it is present, removes it if it is
3118 * @example $("p").toggleClass("selected")
3119 * @before <p>Hello</p><p class="selected">Hello Again</p>
3120 * @result [ <p class="selected">Hello</p>, <p>Hello Again</p> ]
3124 * @param String class A CSS class with which to toggle the elements
3127 toggleClass: function( c ){
3128 jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this,c);
3132 * Removes all matched elements from the DOM. This does NOT remove them from the
3133 * jQuery object, allowing you to use the matched elements further.
3135 * @example $("p").remove();
3136 * @before <p>Hello</p> how are <p>you?</p>
3141 * @cat DOM/Manipulation
3145 * Removes only elements (out of the list of matched elements) that match
3146 * the specified jQuery expression. This does NOT remove them from the
3147 * jQuery object, allowing you to use the matched elements further.
3149 * @example $("p").remove(".hello");
3150 * @before <p class="hello">Hello</p> how are <p>you?</p>
3151 * @result how are <p>you?</p>
3155 * @param String expr A jQuery expression to filter elements by.
3156 * @cat DOM/Manipulation
3158 remove: function(a){
3159 if ( !a || jQuery.filter( a, [this] ).r )
3160 this.parentNode.removeChild( this );
3164 * Removes all child nodes from the set of matched elements.
3166 * @example $("p").empty()
3167 * @before <p>Hello, <span>Person</span> <a href="#">and person</a></p>
3168 * @result [ <p></p> ]
3172 * @cat DOM/Manipulation
3175 while ( this.firstChild )
3176 this.removeChild( this.firstChild );
3180 * Binds a handler to a particular event (like click) for each matched element.
3181 * The event handler is passed an event object that you can use to prevent
3182 * default behaviour. To stop both default action and event bubbling, your handler
3183 * has to return false.
3185 * @example $("p").bind( "click", function() {
3186 * alert( $(this).text() );
3188 * @before <p>Hello</p>
3189 * @result alert("Hello")
3191 * @example $("form").bind( "submit", function() { return false; } )
3192 * @desc Cancel a default action and prevent it from bubbling by returning false
3193 * from your function.
3195 * @example $("form").bind( "submit", function(event) {
3196 * event.preventDefault();
3198 * @desc Cancel only the default action by using the preventDefault method.
3201 * @example $("form").bind( "submit", function(event) {
3202 * event.stopPropagation();
3204 * @desc Stop only an event from bubbling by using the stopPropagation method.
3208 * @param String type An event type
3209 * @param Function fn A function to bind to the event on each of the set of matched elements
3212 bind: function( type, fn ) {
3213 if ( fn.constructor == String )
3214 fn = new Function("e", ( !fn.indexOf(".") ? "jQuery(this)" : "return " ) + fn);
3215 jQuery.event.add( this, type, fn );
3219 * The opposite of bind, removes a bound event from each of the matched
3220 * elements. You must pass the identical function that was used in the original
3223 * @example $("p").unbind( "click", function() { alert("Hello"); } )
3224 * @before <p onclick="alert('Hello');">Hello</p>
3225 * @result [ <p>Hello</p> ]
3229 * @param String type An event type
3230 * @param Function fn A function to unbind from the event on each of the set of matched elements
3235 * Removes all bound events of a particular type from each of the matched
3238 * @example $("p").unbind( "click" )
3239 * @before <p onclick="alert('Hello');">Hello</p>
3240 * @result [ <p>Hello</p> ]
3244 * @param String type An event type
3249 * Removes all bound events from each of the matched elements.
3251 * @example $("p").unbind()
3252 * @before <p onclick="alert('Hello');">Hello</p>
3253 * @result [ <p>Hello</p> ]
3259 unbind: function( type, fn ) {
3260 jQuery.event.remove( this, type, fn );
3264 * Trigger a type of event on every matched element.
3266 * @example $("p").trigger("click")
3267 * @before <p click="alert('hello')">Hello</p>
3268 * @result alert('hello')
3272 * @param String type An event type to trigger.
3275 trigger: function( type, data ) {
3276 jQuery.event.trigger( type, data, this );