2 * jQuery - New Wave Javascript
4 * Copyright (c) 2006 John Resig (jquery.com)
5 * Licensed under the MIT License:
6 * http://www.opensource.org/licenses/mit-license.php
12 // Global undefined variable
13 window.undefined = window.undefined;
16 * Create a new jQuery Object
19 function jQuery(a,c) {
21 // Shortcut for document ready (because $(document).each() is silly)
22 if ( a && a.constructor == Function && jQuery.fn.ready )
23 return jQuery(document).ready(a);
25 // Make sure t hat a selection was provided
26 a = a || jQuery.context || document;
29 * Handle support for overriding other $() functions. Way too many libraries
30 * provide this function to simply ignore it and overwrite it.
33 // Check to see if this is a possible collision case
34 if ( jQuery._$ && !c && a.constructor == String &&
36 // Make sure that the expression is a colliding one
37 !/[^a-zA-Z0-9_-]/.test(a) &&
39 // and that there are no elements that match it
40 // (this is the one truly ambiguous case)
41 !document.getElementsByTagName(a).length )
43 // Use the default method, in case it works some voodoo
44 return jQuery._$( a );
47 // Watch for when a jQuery object is passed as the selector
51 // Watch for when a jQuery object is passed at the context
53 return jQuery(c.get()).find(a);
55 // If the context is global, return a new object
57 return new jQuery(a,c);
59 // Handle HTML strings
60 var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
61 if ( m ) a = jQuery.clean( [ m[1] ] );
63 // Watch for when an array is passed in
64 this.get( a.constructor == Array || a.length && a[0] != undefined && a[0].nodeType ?
65 // Assume that it's an array of DOM Elements
66 jQuery.merge( a, [] ) :
68 // Find the matching elements and save them for later
69 jQuery.find( a, c ) );
71 var fn = arguments[ arguments.length - 1 ];
72 if ( fn && fn.constructor == Function )
76 // Map over the $ in case of overwrite
80 // Map the jQuery namespace to the '$' one
83 jQuery.fn = jQuery.prototype = {
85 * The current SVN version of jQuery.
95 * The number of elements currently matched.
97 * @example $("img").length;
98 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
107 * The number of elements currently matched.
109 * @example $("img").size();
110 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
121 * Access all matched elements. This serves as a backwards-compatible
122 * way of accessing all matched elements (other than the jQuery object
123 * itself, which is, in fact, an array of elements).
125 * @example $("img").get();
126 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
127 * @result [ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
130 * @type Array<Element>
134 * Access a single matched element. <tt>num</tt> is used to access the
135 * <tt>num</tt>th element matched.
137 * @example $("img").get(1);
138 * @before <img src="test1.jpg"/> <img src="test2.jpg"/>
139 * @result [ <img src="test1.jpg"/> ]
143 * @param Number num Access the element in the <tt>num</tt>th position.
147 * Set the jQuery object to an array of elements.
149 * @example $("img").get([ document.body ]);
150 * @result $("img").get() == [ document.body ]
155 * @param Elements elems An array of elements
157 get: function( num ) {
158 // Watch for when an array (of elements) is passed in
159 if ( num && num.constructor == Array ) {
161 // Use a tricky hack to make the jQuery object
162 // look and feel like an array
164 [].push.apply( this, num );
168 return num == undefined ?
170 // Return a 'clean' array
171 jQuery.map( this, function(a){ return a } ) :
173 // Return just the object
178 * Execute a function within the context of every matched element.
179 * This means that every time the passed-in function is executed
180 * (which is once for every element matched) the 'this' keyword
181 * points to the specific element.
183 * Additionally, the function, when executed, is passed a single
184 * argument representing the position of the element in the matched
187 * @example $("img").each(function(){ this.src = "test.jpg"; });
188 * @before <img/> <img/>
189 * @result <img src="test.jpg"/> <img src="test.jpg"/>
193 * @param Function fn A function to execute
195 each: function( fn, args ) {
196 // Iterate through all of the matched elements
197 for ( var i = 0; i < this.length; i++ )
199 // Execute the function within the context of each element
200 fn.apply( this[i], args || [i] );
206 * Access a property on the first matched element.
207 * This method makes it easy to retreive a property value
208 * from the first matched element.
210 * @example $("img").attr("src");
211 * @before <img src="test.jpg"/>
216 * @param String name The name of the property to access.
220 * Set a hash of key/value object properties to all matched elements.
221 * This serves as the best way to set a large number of properties
222 * on all matched elements.
224 * @example $("img").attr({ src: "test.jpg", alt: "Test Image" });
226 * @result <img src="test.jpg" alt="Test Image"/>
230 * @param Hash prop A set of key/value pairs to set as object properties.
234 * Set a single property to a value, on all matched elements.
236 * @example $("img").attr("src","test.jpg");
238 * @result <img src="test.jpg"/>
242 * @param String key The name of the property to set.
243 * @param Object value The value to set the property to.
245 attr: function( key, value, type ) {
246 // Check to see if we're setting style values
247 return key.constructor != String || value ?
248 this.each(function(){
249 // See if we're setting a hash of styles
250 if ( value == undefined )
251 // Set all the styles
252 for ( var prop in key )
254 type ? this.style : this,
258 // See if we're setting a single key/value style
261 type ? this.style : this,
266 // Look for the case where we're accessing a style value
267 jQuery[ type || "attr" ]( this[0], key );
271 * Access a style property on the first matched element.
272 * This method makes it easy to retreive a style property value
273 * from the first matched element.
275 * @example $("p").css("red");
276 * @before <p style="color:red;">Test Paragraph.</p>
281 * @param String name The name of the property to access.
285 * Set a hash of key/value style properties to all matched elements.
286 * This serves as the best way to set a large number of style properties
287 * on all matched elements.
289 * @example $("p").css({ color: "red", background: "blue" });
290 * @before <p>Test Paragraph.</p>
291 * @result <p style="color:red; background:blue;">Test Paragraph.</p>
295 * @param Hash prop A set of key/value pairs to set as style properties.
299 * Set a single style property to a value, on all matched elements.
301 * @example $("p").css("color","red");
302 * @before <p>Test Paragraph.</p>
303 * @result <p style="color:red;">Test Paragraph.</p>
307 * @param String key The name of the property to set.
308 * @param Object value The value to set the property to.
310 css: function( key, value ) {
311 return this.attr( key, value, "curCSS" );
315 * Retreive the text contents of all matched elements. The result is
316 * a string that contains the combined text contents of all matched
317 * elements. This method works on both HTML and XML documents.
319 * @example $("p").text();
320 * @before <p>Test Paragraph.</p>
321 * @result Test Paragraph.
329 for ( var j = 0; j < e.length; j++ ) {
330 var r = e[j].childNodes;
331 for ( var i = 0; i < r.length; i++ )
332 t += r[i].nodeType != 1 ?
333 r[i].nodeValue : jQuery.fn.text([ r[i] ]);
339 * Wrap all matched elements with a structure of other elements.
340 * This wrapping process is most useful for injecting additional
341 * stucture into a document, without ruining the original semantic
342 * qualities of a document.
344 * The way that is works is that it goes through the first element argument
345 * provided and finds the deepest element within the structure - it is that
346 * element that will en-wrap everything else.
348 * @example $("p").wrap("<div class='wrap'></div>");
349 * @before <p>Test Paragraph.</p>
350 * @result <div class='wrap'><p>Test Paragraph.</p></div>
354 * @any String html A string of HTML, that will be created on the fly and wrapped around the target.
355 * @any Element elem A DOM element that will be wrapped.
356 * @any Array<Element> elems An array of elements, the first of which will be wrapped.
357 * @any Object obj Any object, converted to a string, then a text node.
360 // The elements to wrap the target around
361 var a = jQuery.clean(arguments);
363 // Wrap each of the matched elements individually
364 return this.each(function(){
365 // Clone the structure that we're using to wrap
366 var b = a[0].cloneNode(true);
368 // Insert it before the element to be wrapped
369 this.parentNode.insertBefore( b, this );
371 // Find he deepest point in the wrap structure
372 while ( b.firstChild )
375 // Move the matched element to within the wrap structure
376 b.appendChild( this );
381 * Append any number of elements to the inside of all matched elements.
382 * This operation is similar to doing an <tt>appendChild</tt> to all the
383 * specified elements, adding them into the document.
385 * @example $("p").append("<b>Hello</b>");
386 * @before <p>I would like to say: </p>
387 * @result <p>I would like to say: <b>Hello</b></p>
391 * @any String html A string of HTML, that will be created on the fly and appended to the target.
392 * @any Element elem A DOM element that will be appended.
393 * @any Array<Element> elems An array of elements, all of which will be appended.
394 * @any Object obj Any object, converted to a string, then a text node.
397 return this.domManip(arguments, true, 1, function(a){
398 this.appendChild( a );
403 * Prepend any number of elements to the inside of all matched elements.
404 * This operation is the best way to insert a set of elements inside, at the
405 * beginning, of all the matched element.
407 * @example $("p").prepend("<b>Hello</b>");
408 * @before <p>, how are you?</p>
409 * @result <p><b>Hello</b>, how are you?</p>
413 * @any String html A string of HTML, that will be created on the fly and prepended to the target.
414 * @any Element elem A DOM element that will be prepended.
415 * @any Array<Element> elems An array of elements, all of which will be prepended.
416 * @any Object obj Any object, converted to a string, then a text node.
418 prepend: function() {
419 return this.domManip(arguments, true, -1, function(a){
420 this.insertBefore( a, this.firstChild );
425 * Insert any number of elements before each of the matched elements.
427 * @example $("p").before("<b>Hello</b>");
428 * @before <p>how are you?</p>
429 * @result <b>Hello</b><p>how are you?</p>
433 * @any String html A string of HTML, that will be created on the fly and inserted.
434 * @any Element elem A DOM element that will beinserted.
435 * @any Array<Element> elems An array of elements, all of which will be inserted.
436 * @any Object obj Any object, converted to a string, then a text node.
439 return this.domManip(arguments, false, 1, function(a){
440 this.parentNode.insertBefore( a, this );
445 * Insert any number of elements after each of the matched elements.
447 * @example $("p").after("<p>I'm doing fine.</p>");
448 * @before <p>How are you?</p>
449 * @result <p>How are you?</p><p>I'm doing fine.</p>
453 * @any String html A string of HTML, that will be created on the fly and inserted.
454 * @any Element elem A DOM element that will beinserted.
455 * @any Array<Element> elems An array of elements, all of which will be inserted.
456 * @any Object obj Any object, converted to a string, then a text node.
459 return this.domManip(arguments, false, -1, function(a){
460 this.parentNode.insertBefore( a, this.nextSibling );
465 * End the most recent 'destructive' operation, reverting the list of matched elements
466 * back to its previous state. After an end operation, the list of matched elements will
467 * revert to the last state of matched elements.
469 * @example $("p").find("span").end();
470 * @before <p><span>Hello</span>, how are you?</p>
471 * @result $("p").find("span").end() == [ <p>...</p> ]
477 return this.get( this.stack.pop() );
481 * Searches for all elements that match the specified expression.
482 * This method is the optimal way of finding additional descendant
483 * elements with which to process.
485 * All searching is done using a jQuery expression. The expression can be
486 * written using CSS 1-3 Selector syntax, or basic XPath.
488 * @example $("p").find("span");
489 * @before <p><span>Hello</span>, how are you?</p>
490 * @result $("p").find("span") == [ <span>Hello</span> ]
494 * @param String expr An expression to search with.
497 return this.pushStack( jQuery.map( this, function(a){
498 return jQuery.find(t,a);
503 * Removes all elements from the set of matched elements that do not
504 * match the specified expression. This method is used to narrow down
505 * the results of a search.
507 * All searching is done using a jQuery expression. The expression
508 * can be written using CSS 1-3 Selector syntax, or basic XPath.
510 * @example $("p").filter(".selected")
511 * @before <p class="selected">Hello</p><p>How are you?</p>
512 * @result $("p").filter(".selected") == [ <p class="selected">Hello</p> ]
516 * @param String expr An expression to search with.
520 * Removes all elements from the set of matched elements that do not
521 * match at least one of the expressions passed to the function. This
522 * method is used when you want to filter the set of matched elements
523 * through more than one expression.
525 * Elements will be retained in the jQuery object if they match at
526 * least one of the expressions passed.
528 * @example $("p").filter([".selected", ":first"])
529 * @before <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
530 * @result $("p").filter([".selected", ":first"]) == [ <p>Hello</p>, <p class="selected">And Again</p> ]
534 * @param Array<String> exprs A set of expressions to evaluate against
536 filter: function(t) {
537 return this.pushStack(
538 t.constructor == Array &&
539 jQuery.map(this,function(a){
540 for ( var i = 0; i < t.length; i++ )
541 if ( jQuery.filter(t[i],[a]).r.length )
545 t.constructor == Boolean &&
546 ( t ? this.get() : [] ) ||
548 t.constructor == Function &&
549 jQuery.grep( this, t ) ||
551 jQuery.filter(t,this).r, arguments );
555 * Removes the specified Element from the set of matched elements. This
556 * method is used to remove a single Element from a jQuery object.
558 * @example $("p").not( document.getElementById("selected") )
559 * @before <p>Hello</p><p id="selected">Hello Again</p>
560 * @result [ <p>Hello</p> ]
564 * @param Element el An element to remove from the set
568 * Removes elements matching the specified expression from the set
569 * of matched elements. This method is used to remove one or more
570 * elements from a jQuery object.
572 * @example $("p").not("#selected")
573 * @before <p>Hello</p><p id="selected">Hello Again</p>
574 * @result [ <p>Hello</p> ]
578 * @param String expr An expression with which to remove matching elements
581 return this.pushStack( t.constructor == String ?
582 jQuery.filter(t,this,false).r :
583 jQuery.grep(this,function(a){ return a != t; }), arguments );
587 * Adds the elements matched by the expression to the jQuery object. This
588 * can be used to concatenate the result sets of two expressions.
590 * @example $("p").add("span")
591 * @before <p>Hello</p><p><span>Hello Again</span></p>
592 * @result [ <p>Hello</p>, <span>Hello Again</span> ]
596 * @param String expr An expression whose matched elements are added
600 * Adds each of the Elements in the array to the set of matched elements.
601 * This is used to add a set of Elements to a jQuery object.
603 * @example $("p").add([document.getElementById("a"), document.getElementById("b")])
604 * @before <p>Hello</p><p><span id="a">Hello Again</span><span id="b">And Again</span></p>
605 * @result [ <p>Hello</p>, <span id="a">Hello Again</span>, <span id="b">And Again</span> ]
609 * @param Array<Element> els An array of Elements to add
613 * Adds a single Element to the set of matched elements. This is used to
614 * add a single Element to a jQuery object.
616 * @example $("p").add( document.getElementById("a") )
617 * @before <p>Hello</p><p><span id="a">Hello Again</span></p>
618 * @result [ <p>Hello</p>, <span id="a">Hello Again</span> ]
622 * @param Element el An Element to add
625 return this.pushStack( jQuery.merge( this, t.constructor == String ?
626 jQuery.find(t) : t.constructor == Array ? t : [t] ), arguments );
630 * A wrapper function for each() to be used by append and prepend.
631 * Handles cases where you're trying to modify the inner contents of
632 * a table, when you actually need to work with the tbody.
635 * @param {String} expr The expression with which to filter
639 return expr ? jQuery.filter(expr,this).r.length > 0 : this.length > 0;
648 * @param Boolean table
650 * @param Function fn The function doing the DOM manipulation.
653 domManip: function(args, table, dir, fn){
654 var clone = this.size() > 1;
655 var a = jQuery.clean(args);
657 return this.each(function(){
660 if ( table && this.nodeName == "TABLE" ) {
661 var tbody = this.getElementsByTagName("tbody");
663 if ( !tbody.length ) {
664 obj = document.createElement("tbody");
665 this.appendChild( obj );
670 for ( var i = ( dir < 0 ? a.length - 1 : 0 );
671 i != ( dir < 0 ? dir : a.length ); i += dir ) {
672 fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );
687 pushStack: function(a,args) {
688 var fn = args && args[args.length-1];
690 if ( !fn || fn.constructor != Function ) {
691 if ( !this.stack ) this.stack = [];
692 this.stack.push( this.get() );
695 var old = this.get();
697 if ( fn.constructor == Function )
698 return this.each( fn );
705 extend: function(obj,prop) {
706 if ( !prop ) { prop = obj; obj = this; }
707 for ( var i in prop ) obj[i] = prop[i];
712 jQuery.extend = jQuery.fn.extend;
715 var b = navigator.userAgent.toLowerCase();
717 // Figure out what browser is being used
719 safari: /webkit/.test(b),
720 opera: /opera/.test(b),
721 msie: /msie/.test(b) && !/opera/.test(b),
722 mozilla: /mozilla/.test(b) && !/compatible/.test(b)
725 // Check to see if the W3C box model is being used
726 jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
730 * Get a set of elements containing the unique parents of the matched
733 * @example $("p").parent()
734 * @before <div><p>Hello</p><p>Hello</p></div>
735 * @result [ <div><p>Hello</p><p>Hello</p></div> ]
742 * Get a set of elements containing the unique parents of the matched
743 * set of elements, and filtered by an expression.
745 * @example $("p").parent(".selected")
746 * @before <div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>
747 * @result [ <div class="selected"><p>Hello Again</p></div> ]
751 * @param String expr An expression to filter the parents with
753 parent: "a.parentNode",
756 * Get a set of elements containing the unique ancestors of the matched
759 * @example $("span").ancestors()
760 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
761 * @result [ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
768 * Get a set of elements containing the unique ancestors of the matched
769 * set of elements, and filtered by an expression.
771 * @example $("span").ancestors("p")
772 * @before <html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
773 * @result [ <p><span>Hello</span></p> ]
777 * @param String expr An expression to filter the ancestors with
779 ancestors: jQuery.parents,
782 * A synonym for ancestors
784 parents: jQuery.parents,
787 * Get a set of elements containing the unique next siblings of each of the
788 * matched set of elements.
790 * It only returns the very next sibling, not all next siblings.
792 * @example $("p").next()
793 * @before <p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
794 * @result [ <p>Hello Again</p>, <div><span>And Again</span></div> ]
801 * Get a set of elements containing the unique next siblings of each of the
802 * matched set of elements, and filtered by an expression.
804 * It only returns the very next sibling, not all next siblings.
806 * @example $("p").next(".selected")
807 * @before <p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>
808 * @result [ <p class="selected">Hello Again</p> ]
812 * @param String expr An expression to filter the next Elements with
814 next: "jQuery.sibling(a).next",
817 * Get a set of elements containing the unique previous siblings of each of the
818 * matched set of elements.
820 * It only returns the immediately previous sibling, not all previous siblings.
822 * @example $("p").previous()
823 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
824 * @result [ <div><span>Hello Again</span></div> ]
831 * Get a set of elements containing the unique previous siblings of each of the
832 * matched set of elements, and filtered by an expression.
834 * It only returns the immediately previous sibling, not all previous siblings.
836 * @example $("p").previous("selected")
837 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
838 * @result [ <div><span>Hello</span></div> ]
842 * @param String expr An expression to filter the previous Elements with
844 prev: "jQuery.sibling(a).prev",
847 * Get a set of elements containing all of the unique siblings of each of the
848 * matched set of elements.
850 * @example $("div").siblings()
851 * @before <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
852 * @result [ <p>Hello</p>, <p>And Again</p> ]
859 * Get a set of elements containing all of the unique siblings of each of the
860 * matched set of elements, and filtered by an expression.
862 * @example $("div").siblings("selected")
863 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
864 * @result [ <p class="selected">Hello Again</p> ]
868 * @param String expr An expression to filter the sibling Elements with
870 siblings: jQuery.sibling
873 for ( var i in axis ) new function(){
875 jQuery.fn[ i ] = function(a) {
876 var ret = jQuery.map(this,t);
877 if ( a && a.constructor == String )
878 ret = jQuery.filter(a,ret).r;
879 return this.pushStack( ret, arguments );
883 // appendTo, prependTo, beforeTo, afterTo
885 var to = ["append","prepend","before","after"];
887 for ( var i = 0; i < to.length; i++ ) new function(){
889 jQuery.fn[ n + "To" ] = function(){
891 return this.each(function(){
892 for ( var i = 0; i < a.length; i++ )
900 * Displays each of the set of matched elements if they are hidden.
902 * @example $("p").show()
903 * @before <p style="display: none">Hello</p>
904 * @result [ <p style="display: block">Hello</p> ]
910 this.style.display = this.oldblock ? this.oldblock : "";
911 if ( jQuery.css(this,"display") == "none" )
912 this.style.display = "block";
916 * Hides each of the set of matched elements if they are shown.
918 * @example $("p").hide()
919 * @before <p>Hello</p>
920 * @result [ <p style="display: none">Hello</p> ]
926 this.oldblock = jQuery.css(this,"display");
927 if ( this.oldblock == "none" )
928 this.oldblock = "block";
929 this.style.display = "none";
933 * Toggles each of the set of matched elements. If they are shown,
934 * toggle makes them hidden. If they are hidden, toggle
937 * @example $("p").toggle()
938 * @before <p>Hello</p><p style="display: none">Hello Again</p>
939 * @result [ <p style="display: none">Hello</p>, <p style="display: block">Hello Again</p> ]
945 var d = jQuery.css(this,"display");
946 $(this)[ !d || d == "none" ? "show" : "hide" ]();
950 * Adds the specified class to each of the set of matched elements.
952 * @example ("p").addClass("selected")
953 * @before <p>Hello</p>
954 * @result [ <p class="selected">Hello</p> ]
958 * @param String class A CSS class to add to the elements
960 addClass: function(c){
961 jQuery.className.add(this,c);
965 * The opposite of addClass. Removes the specified class from the
966 * set of matched elements.
968 * @example ("p").removeClass("selected")
969 * @before <p class="selected">Hello</p>
970 * @result [ <p>Hello</p> ]
974 * @param String class A CSS class to remove from the elements
976 removeClass: function(c){
977 jQuery.className.remove(this,c);
981 * Adds the specified class if it is present. Remove it if it is
984 * @example ("p").toggleClass("selected")
985 * @before <p>Hello</p><p class="selected">Hello Again</p>
986 * @result [ <p class="selected">Hello</p>, <p>Hello Again</p> ]
990 * @param String class A CSS class with which to toggle the elements
992 toggleClass: function( c ){
993 jQuery.className[ jQuery.className.has(this,c) ? "remove" : "add" ](this,c);
1000 if ( !a || jQuery.filter( [this], a ).r )
1001 this.parentNode.removeChild( this );
1005 * Removes all child nodes from the set of matched elements.
1007 * @example ("p").empty()
1008 * @before <p>Hello, <span>Person</span> <a href="#">and person</a></p>
1009 * @result [ <p></p> ]
1015 while ( this.firstChild )
1016 this.removeChild( this.firstChild );
1020 * Binds a particular event (like click) to a each of a set of match elements.
1022 * @example $("p").bind( "click", function() { alert("Hello"); } )
1023 * @before <p>Hello</p>
1024 * @result [ <p>Hello</p> ]
1026 * Cancel a default action and prevent it from bubbling by returning false
1027 * from your function.
1029 * @example $("form").bind( "submit", function() { return false; } )
1031 * Cancel a default action by using the preventDefault method.
1033 * @example $("form").bind( "submit", function() { e.preventDefault(); } )
1035 * Stop an event from bubbling by using the stopPropogation method.
1037 * @example $("form").bind( "submit", function() { e.stopPropogation(); } )
1041 * @param String type An event type
1042 * @param Function fn A function to bind to the event on each of the set of matched elements
1044 bind: function( type, fn ) {
1045 if ( fn.constructor == String )
1046 fn = new Function("e", ( !fn.indexOf(".") ? "$(this)" : "return " ) + fn);
1047 jQuery.event.add( this, type, fn );
1051 * The opposite of bind. Removes a bound event from each of a set of matched
1052 * elements. You must pass the identical function that was used in the original
1055 * @example $("p").unbind( "click", function() { alert("Hello"); } )
1056 * @before <p>Hello</p>
1057 * @result [ <p>Hello</p> ]
1061 * @param String type An event type
1062 * @param Function fn A function to unbind from the event on each of the set of matched elements
1064 unbind: function( type, fn ) {
1065 jQuery.event.remove( this, type, fn );
1069 * Trigger a particular event.
1071 * @example $("p").trigger("click")
1072 * @before <p>Hello</p>
1073 * @result [ <p>Hello</p> ]
1077 * @param String type An event type
1079 trigger: function( type ) {
1080 jQuery.event.trigger( this, type );
1084 for ( var i in each ) new function() {
1086 jQuery.fn[ i ] = function() {
1087 return this.each( n, arguments );
1103 for ( var i in attr ) new function() {
1104 var n = attr[i] || i;
1105 jQuery.fn[ i ] = function(h) {
1106 return h == undefined ?
1107 this.length ? this[0][n] : null :
1112 var css = "width,height,top,left,position,float,overflow,color,background".split(",");
1114 for ( var i in css ) new function() {
1116 jQuery.fn[ i ] = function(h) {
1117 return h == undefined ?
1118 ( this.length ? jQuery.css( this[0], n ) : null ) :
1128 if (jQuery.className.has(o,c)) return;
1129 o.className += ( o.className ? " " : "" ) + c;
1131 remove: function(o,c){
1132 o.className = !c ? "" :
1133 o.className.replace(
1134 new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");
1136 has: function(e,a) {
1139 return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
1144 * Swap in/out style options.
1147 swap: function(e,o,f) {
1148 for ( var i in o ) {
1149 e.style["old"+i] = e.style[i];
1154 e.style[i] = e.style["old"+i];
1157 css: function(e,p) {
1158 if ( p == "height" || p == "width" ) {
1159 var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
1161 for ( var i in d ) {
1162 old["padding" + d[i]] = 0;
1163 old["border" + d[i] + "Width"] = 0;
1166 jQuery.swap( e, old, function() {
1167 if (jQuery.css(e,"display") != "none") {
1168 oHeight = e.offsetHeight;
1169 oWidth = e.offsetWidth;
1171 jQuery.swap( e, { visibility: "hidden", position: "absolute", display: "" },
1173 oHeight = e.clientHeight;
1174 oWidth = e.clientWidth;
1178 return p == "height" ? oHeight : oWidth;
1181 return jQuery.curCSS( e, p );
1184 curCSS: function(e,p) {
1189 else if (e.currentStyle)
1190 r = e.currentStyle[p];
1191 else if (document.defaultView && document.defaultView.getComputedStyle) {
1192 p = p.replace(/([A-Z])/g,"-$1").toLowerCase();
1193 var s = document.defaultView.getComputedStyle(e,"");
1194 r = s ? s.getPropertyValue(p) : null;
1200 clean: function(a) {
1202 for ( var i = 0; i < a.length; i++ ) {
1203 if ( a[i].constructor == String ) {
1205 if ( !a[i].indexOf("<tr") ) {
1207 a[i] = "<table>" + a[i] + "</table>";
1208 } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
1210 a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
1213 var div = document.createElement("div");
1214 div.innerHTML = a[i];
1217 div = div.firstChild.firstChild;
1218 if ( td ) div = div.firstChild;
1221 for ( var j = 0; j < div.childNodes.length; j++ )
1222 r.push( div.childNodes[j] );
1223 } else if ( a[i].jquery || a[i].length && !a[i].nodeType )
1224 for ( var k = 0; k < a[i].length; k++ )
1226 else if ( a[i] !== null )
1227 r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) );
1233 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
1234 "#": "a.getAttribute('id')&&a.getAttribute('id')==m[2]",
1242 last: "i==r.length-1",
1247 "first-child": "jQuery.sibling(a,0).cur",
1248 "last-child": "jQuery.sibling(a,0).last",
1249 "only-child": "jQuery.sibling(a).length==1",
1252 parent: "a.childNodes.length",
1253 empty: "!a.childNodes.length",
1256 contains: "(a.innerText||a.innerHTML).indexOf(m[3])>=0",
1259 visible: "a.type!='hidden'&&jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!='hidden'",
1260 hidden: "a.type=='hidden'||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')=='hidden'",
1263 enabled: "!a.disabled",
1264 disabled: "a.disabled",
1265 checked: "a.checked"
1267 ".": "jQuery.className.has(a,m[2])",
1271 "^=": "!z.indexOf(m[4])",
1272 "$=": "z.substr(z.length - m[4].length,m[4].length)==m[4]",
1273 "*=": "z.indexOf(m[4])>=0",
1276 "[": "jQuery.find(m[2],a).length"
1280 "\\.\\.|/\\.\\.", "a.parentNode",
1281 ">|/", "jQuery.sibling(a.firstChild)",
1282 "\\+", "jQuery.sibling(a).next",
1285 var s = jQuery.sibling(a);
1287 for ( var i = s.n; i < s.length; i++ )
1293 find: function( t, context ) {
1294 // Make sure that the context is a DOM Element
1295 if ( context && context.getElementsByTagName == undefined )
1298 // Set the correct context (if none is provided)
1299 context = context || jQuery.context || document;
1301 if ( t.constructor != String ) return [t];
1303 if ( !t.indexOf("//") ) {
1304 context = context.documentElement;
1305 t = t.substr(2,t.length);
1306 } else if ( !t.indexOf("/") ) {
1307 context = context.documentElement;
1308 t = t.substr(1,t.length);
1309 // FIX Assume the root element is right :(
1310 if ( t.indexOf("/") >= 1 )
1311 t = t.substr(t.indexOf("/"),t.length);
1314 var ret = [context];
1318 while ( t.length > 0 && last != t ) {
1322 t = jQuery.trim(t).replace( /^\/\//i, "" );
1324 var foundToken = false;
1326 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
1327 var re = new RegExp("^(" + jQuery.token[i] + ")");
1331 r = ret = jQuery.map( ret, jQuery.token[i+1] );
1332 t = jQuery.trim( t.replace( re, "" ) );
1337 if ( !foundToken ) {
1338 if ( !t.indexOf(",") || !t.indexOf("|") ) {
1339 if ( ret[0] == context ) ret.shift();
1340 done = jQuery.merge( done, ret );
1341 r = ret = [context];
1342 t = " " + t.substr(1,t.length);
1344 var re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
1345 var m = re2.exec(t);
1347 if ( m[1] == "#" ) {
1348 // Ummm, should make this work in all XML docs
1349 var oid = document.getElementById(m[2]);
1350 r = ret = oid ? [oid] : [];
1351 t = t.replace( re2, "" );
1353 if ( !m[2] || m[1] == "." ) m[2] = "*";
1355 for ( var i = 0; i < ret.length; i++ )
1356 r = jQuery.merge( r,
1358 jQuery.getAll(ret[i]) :
1359 ret[i].getElementsByTagName(m[2])
1366 var val = jQuery.filter(t,r);
1368 t = jQuery.trim(val.t);
1372 if ( ret && ret[0] == context ) ret.shift();
1373 done = jQuery.merge( done, ret );
1378 getAll: function(o,r) {
1380 var s = o.childNodes;
1381 for ( var i = 0; i < s.length; i++ )
1382 if ( s[i].nodeType == 1 ) {
1384 jQuery.getAll( s[i], r );
1389 attr: function(o,a,v){
1390 if ( a && a.constructor == String ) {
1393 "class": "className",
1397 a = (fix[a] && fix[a].replace && fix[a] || a)
1398 .replace(/-([a-z])/ig,function(z,b){
1399 return b.toUpperCase();
1402 if ( v != undefined ) {
1404 if ( o.setAttribute && a != "disabled" )
1405 o.setAttribute(a,v);
1408 return o[a] || o.getAttribute && o.getAttribute(a) || "";
1413 // The regular expressions that power the parsing engine
1415 // Match: [@value='test'], [@foo]
1416 [ "\\[ *(@)S *([!*$^=]*) *Q\\]", 1 ],
1418 // Match: [div], [div p]
1421 // Match: :contains('foo')
1422 [ "(:)S\\(Q\\)", 0 ],
1424 // Match: :even, :last-chlid
1428 filter: function(t,r,not) {
1429 // Figure out if we're doing regular, or inverse, filtering
1430 var g = not !== false ? jQuery.grep :
1431 function(a,f) {return jQuery.grep(a,f,true);};
1433 while ( t && /^[a-z[({<*:.#]/i.test(t) ) {
1435 var p = jQuery.parse;
1437 for ( var i = 0; i < p.length; i++ ) {
1438 var re = new RegExp( "^" + p[i][0]
1440 // Look for a string-like sequence
1441 .replace( 'S', "([a-z*_-][a-z0-9_-]*)" )
1443 // Look for something (optionally) enclosed with quotes
1444 .replace( 'Q', " *'?\"?([^'\"]*)'?\"? *" ), "i" );
1446 var m = re.exec( t );
1449 // Re-organize the match
1451 m = ["", m[1], m[3], m[2], m[4]];
1453 // Remove what we just matched
1454 t = t.replace( re, "" );
1460 // :not() is a special case that can be optomized by
1461 // keeping it out of the expression list
1462 if ( m[1] == ":" && m[2] == "not" )
1463 r = jQuery.filter(m[3],r,false).r;
1465 // Otherwise, find the expression to execute
1467 var f = jQuery.expr[m[1]];
1468 if ( f.constructor != String )
1469 f = jQuery.expr[m[1]][m[2]];
1471 // Build a custom macro to enclose it
1472 eval("f = function(a,i){" +
1473 ( m[1] == "@" ? "z=jQuery.attr(a,m[3]);" : "" ) +
1474 "return " + f + "}");
1476 // Execute it against the current filter
1481 // Return an array of filtered elements (r)
1482 // and the modified expression string (t)
1483 return { r: r, t: t };
1487 * Remove the whitespace from the beginning and end of a string.
1492 * @param String str The string to trim.
1495 return t.replace(/^\s+|\s+$/g, "");
1499 * All ancestors of a given element.
1503 * @type Array<Element>
1504 * @param Element elem The element to find the ancestors of.
1506 parents: function(a){
1508 var c = a.parentNode;
1509 while ( c && c != document ) {
1517 * All elements on a specified axis.
1522 * @param Element elem The element to find all the siblings of (including itself).
1524 sibling: function(a,n) {
1526 var tmp = a.parentNode.childNodes;
1527 for ( var i = 0; i < tmp.length; i++ ) {
1528 if ( tmp[i].nodeType == 1 )
1529 type.push( tmp[i] );
1531 type.n = type.length - 1;
1533 type.last = type.n == type.length - 1;
1535 n == "even" && type.n % 2 == 0 ||
1536 n == "odd" && type.n % 2 ||
1538 type.prev = type[type.n - 1];
1539 type.next = type[type.n + 1];
1544 * Merge two arrays together, removing all duplicates.
1549 * @param Array a The first array to merge.
1550 * @param Array b The second array to merge.
1552 merge: function(a,b) {
1555 // Move b over to the new array (this helps to avoid
1556 // StaticNodeList instances)
1557 for ( var k = 0; k < b.length; k++ )
1560 // Now check for duplicates between a and b and only
1561 // add the unique items
1562 for ( var i = 0; i < a.length; i++ ) {
1565 // The collision-checking process
1566 for ( var j = 0; j < b.length; j++ )
1570 // If the item is unique, add it
1579 * Remove items that aren't matched in an array. The function passed
1580 * in to this method will be passed two arguments: 'a' (which is the
1581 * array item) and 'i' (which is the index of the item in the array).
1586 * @param Array array The Array to find items in.
1587 * @param Function fn The function to process each item against.
1588 * @param Boolean inv Invert the selection - select the opposite of the function.
1590 grep: function(a,f,s) {
1591 // If a string is passed in for the function, make a function
1592 // for it (a handy shortcut)
1593 if ( f.constructor == String )
1594 f = new Function("a","i","return " + f);
1598 // Go through the array, only saving the items
1599 // that pass the validator function
1600 for ( var i = 0; i < a.length; i++ )
1601 if ( !s && f(a[i],i) || s && !f(a[i],i) )
1608 * Translate all items in array to another array of items. The translation function
1609 * that is provided to this method is passed one argument: 'a' (the item to be
1610 * translated). If an array is returned, that array is mapped out and merged into
1611 * the full array. Additionally, returning 'null' or 'undefined' will delete the item
1612 * from the array. Both of these changes imply that the size of the array may not
1613 * be the same size upon completion, as it was when it started.
1618 * @param Array array The Array to translate.
1619 * @param Function fn The function to process each item against.
1621 map: function(a,f) {
1622 // If a string is passed in for the function, make a function
1623 // for it (a handy shortcut)
1624 if ( f.constructor == String )
1625 f = new Function("a","return " + f);
1629 // Go through the array, translating each of the items to their
1630 // new value (or values).
1631 for ( var i = 0; i < a.length; i++ ) {
1633 if ( t !== null && t != undefined ) {
1634 if ( t.constructor != Array ) t = [t];
1635 r = jQuery.merge( t, r );
1642 * A number of helper functions used for managing events.
1643 * Many of the ideas behind this code orignated from Dean Edwards' addEvent library.
1647 // Bind an event to an element
1648 // Original by Dean Edwards
1649 add: function(element, type, handler) {
1650 // For whatever reason, IE has trouble passing the window object
1651 // around, causing it to be cloned in the process
1652 if ( jQuery.browser.msie && element.setInterval != undefined )
1655 // Make sure that the function being executed has a unique ID
1656 if ( !handler.guid )
1657 handler.guid = this.guid++;
1659 // Init the element's event structure
1660 if (!element.events)
1661 element.events = {};
1663 // Get the current list of functions bound to this event
1664 var handlers = element.events[type];
1666 // If it hasn't been initialized yet
1668 // Init the event handler queue
1669 handlers = element.events[type] = {};
1671 // Remember an existing handler, if it's already there
1672 if (element["on" + type])
1673 handlers[0] = element["on" + type];
1676 // Add the function to the element's handler list
1677 handlers[handler.guid] = handler;
1679 // And bind the global event handler to the element
1680 element["on" + type] = this.handle;
1682 // Remember the function in a global list (for triggering)
1683 if (!this.global[type])
1684 this.global[type] = [];
1685 this.global[type].push( element );
1691 // Detach an event or set of events from an element
1692 remove: function(element, type, handler) {
1694 if (type && element.events[type])
1696 delete element.events[type][handler.guid];
1698 for ( var i in element.events[type] )
1699 delete element.events[type][i];
1701 for ( var j in element.events )
1702 this.remove( element, j );
1705 trigger: function(type,data,element) {
1706 // Touch up the incoming data
1709 // Handle a global trigger
1711 var g = this.global[type];
1713 for ( var i = 0; i < g.length; i++ )
1714 this.trigger( type, data, g[i] );
1716 // Handle triggering a single element
1717 } else if ( element["on" + type] ) {
1718 // Pass along a fake event
1719 data.unshift( this.fix({ type: type, target: element }) );
1721 // Trigger the event
1722 element["on" + type].apply( element, data );
1726 handle: function(event) {
1727 event = event || jQuery.event.fix( window.event );
1729 // If no correct event was found, fail
1730 if ( !event ) return;
1732 var returnValue = true;
1734 var c = this.events[event.type];
1736 for ( var j in c ) {
1737 if ( c[j].apply( this, [event] ) === false ) {
1738 event.preventDefault();
1739 event.stopPropagation();
1740 returnValue = false;
1747 fix: function(event) {
1749 event.preventDefault = function() {
1750 this.returnValue = false;
1753 event.stopPropagation = function() {
1754 this.cancelBubble = true;