&lt;p&gt;You can pass in plain HTML Strings written by hand, create them using some template engine or plugin, or load them via AJAX. When creating single elements use the closing tag or XHTML format. For example, to create a span use $("&lt;span/&gt;"). As of jQuery 1.3 this syntax is completely equivalent to $(document.createElement("span")).&lt;/p&gt; A string of HTML to create on the fly. A document in which the new elements will be created <p> Creates a div element (and all of its contents) dynamically, and appends it to the body element. Internally, an element is created and its innerHTML property set to the given markup. It is therefore both quite flexible and limited. </p> <pre> $("&lt;div&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;/div&gt;").appendTo("body") </pre> This function also accepts XML Documents and Window objects as valid arguments (even though they are not DOM Elements). DOM element(s) to be encapsulated by a jQuery object. <p> Sets the background color of the page to black. </p> <pre> $(document.body).css( "background", "black" ); </pre> <p> Hides all the input elements within a form. </p> <pre> $(myForm.elements).hide() </pre> Allows you to bind a function to be executed when the DOM document has finished loading. This function behaves just like $(document).ready(), in that it should be used to wrap other $() operations on your page that depend on the DOM being ready to be operated on. While this function is, technically, chainable - there really isn't much use for chaining against it. You can have as many $(document).ready events on your page as you like. See ready(Function) for details about the ready event. The function to execute when the DOM is ready. <p> Executes the function when the DOM is ready to be used. </p> <pre> $(function(){ // Document is ready }); </pre> <p> Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias. </p> <pre> jQuery(function($) { // Your code using failsafe $ alias here... }); </pre> The core functionality of jQuery centers around this function. Everything in jQuery is based upon this, or uses this in some way. The most basic use of this function is to pass in an expression (usually consisting of CSS), which then finds all matching elements. By default, if no context is specified, $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of that context. See &lt;a href='Selectors'&gt;Selectors&lt;/a&gt; for the allowed CSS syntax for expressions. An expression to search with. A DOM Element, Document or jQuery to use as context <p> Finds all p elements that are children of a div element. </p> <pre> $("div &gt; p").css("border", "1px solid gray"); </pre> <pre> &lt;p&gt;one&lt;/p&gt; &lt;div&gt;&lt;p&gt;two&lt;/p&gt;&lt;/div&gt; &lt;p&gt;three&lt;/p&gt; </pre> <p> Finds all inputs of type radio within the first form in the document. </p> <pre> $("input:radio", document.forms[0]); </pre> <p> Finds all div elements within an XML document from an AJAX response. </p> <pre> $("div", xml.responseXML); </pre> &lt;p&gt;Should be used in conjunction with selector to determine the exact query used. These two properties are mostly useful to plugin developers.&lt;/p&gt; <p> Determine the exact context used. </p> <pre> $("ul") .append("&lt;li&gt;" + $("ul").context + "&lt;/li&gt;") .append("&lt;li&gt;" + $("ul", document.body).context.nodeName + "&lt;/li&gt;"); </pre> <pre> Context:&lt;ul&gt;&lt;/ul&gt; </pre> <p> Returns the box model for the iframe. </p> <pre> $("p").html("The box model for this iframe is: &lt;span&gt;" + jQuery.boxModel + "&lt;/span&gt;"); </pre> <pre> &lt;p&gt; &lt;/p&gt; </pre> <p> Returns false if the page is in QuirksMode in Internet Explorer </p> <pre> $.boxModel </pre> Available flags are: * safari * opera * msie * mozilla This property is available before the DOM is ready, therefore you can use it to add ready events only for certain browsers. There are situations where object detection is not reliable enough, in such cases it makes sense to use browser detection. A combination of browser and object detection yields quite reliable results. <p> Show the browser info. </p> <pre> jQuery.each(jQuery.browser, function(i, val) { $("&lt;div&gt;" + i + " : &lt;span&gt;" + val + "&lt;/span&gt;") .appendTo(document.body); }); </pre> <pre> &lt;p&gt;Browser info:&lt;/p&gt; </pre> <p> Returns true if the current useragent is some version of Microsoft's Internet Explorer. </p> <pre> $.browser.msie </pre> <p> Alerts "this is safari!" only for safari browsers </p> <pre> if ($.browser.safari) { alert("this is safari!"); } </pre> <p> Alerts "Do stuff for firefox 3" only for firefox 3 browsers. </p> <pre> jQuery.each(jQuery.browser, function(i, val) { if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9") alert("Do stuff for firefox 3") }); </pre> <p> Set a CSS property to specific browser. </p> <pre> jQuery.each(jQuery.browser, function(i) { if($.browser.msie){ $("#div ul li").css("display","inline"); }else{ $("#div ul li").css("display","inline-table"); } }); </pre> Here are some typical results: * Internet Explorer: 6.0, 7.0 * Mozilla/Firefox/Flock/Camino: 1.7.12, 1.8.1.3, 1.9 * Opera: 9.20 * Safari/Webkit: 312.8, 418.9 <p> Returns the browser version. </p> <pre> $("p").html("The browser version is: &lt;span&gt;" + jQuery.browser.version + "&lt;/span&gt;"); </pre> <pre> &lt;p&gt; &lt;/p&gt; </pre> <p> Alerts the version of IE that is being used </p> <pre> if ( $.browser.msie ) alert( $.browser.version ); } </pre> <p> Often you only care about the "major number," the whole number. This can be accomplished with JavaScript's built-in parseInt() function: </p> <pre> if (jQuery.browser.msie) { alert(parseInt(jQuery.browser.version)); } </pre> &lt;p&gt;Setting this property to true will disable all animations from occurring (the effect will happen instantaneously, instead). This may be desirable for a couple reasons:&lt;/p&gt; # You're using jQuery on a low-resource device. # Some of your users are encountering [http://www.jdeegan.phlegethon.org/turn_off_animation.html accessibility problems] with the animations. &lt;p&gt;Animations can be turned back on by setting the property to false.&lt;/p&gt; <p> Run a disabled animation </p> <pre> jQuery.fx.off = true; $("input").click(function(){ $("div").toggle("slow"); }); </pre> <pre> &lt;input type="button" value="Run"/&gt;&lt;div&gt;&lt;/div&gt; </pre> &lt;p&gt;jQuery comes with a number of properties included, you should feel free to add your own. Many of these properties are rather low-level so it's doubtful that they'll be useful in general day-to-day development, but mostly used by plugin and core developers.&lt;/p&gt; &lt;p&gt;The values of all the support properties are determined using feature detection (and do not use any form of browser sniffing). There are some excellent resources that explain how feature detection works:&lt;/p&gt; * http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting * http://yura.thinkweb2.com/cft/ * http://www.jibbering.com/faq/faq_notes/not_browser_detect.html &lt;p&gt;The tests included in jQuery.support are as follows:&lt;/p&gt; * '''boxModel''': Is equal to true if the page and browser are rendering according to the [http://www.w3.org/TR/REC-CSS2/box.html W3C CSS Box Model] (is currently false in IE 6 and 7 when they are in Quirks Mode). This property is null until document ready occurs. * '''cssFloat''': Is equal to true if style.cssFloat is used to access the current CSS float value (is currently false in IE, it uses styleFloat instead). * '''hrefNormalized''': Is equal to true if the browser leaves intact the results from getAttribute("href")(is currently false in IE, the URLs are normalized). * '''htmlSerialize''': Is equal to true if the browser properly serializes link elements when innerHTML is used (is currently false in IE). * '''leadingWhitespace''': Is equal to true if the browser preserves leading whitespace when innerHTML is used (is currently false in IE 6-8). * '''noCloneEvent''': Is equal to true if the browser does not clone event handlers when elements are cloned (is currently false in IE). * '''objectAll''': Is equal to true if doing getElementsByTagName("*") on an object element returns all descendant elements (is currently false in IE 7). * '''opacity''': Is equal to true if a browser can properly interpret the opacity style property (is currently false in IE, it uses alpha filters instead). * '''scriptEval''': Is equal to true if using appendChild/createTextNode to inject inline scripts executes them (is currently false in IE, it uses .text to insert executable scripts). * '''style''': Is equal to true if getAttribute("style") is able to return the inline style specified by an element (is currently false in IE - it uses cssText instead). * '''tbody''': Is equal to true if the browser allows table elements without tbody elements (is currently false in IE, which automatically inserts tbody if it is not present). <p> Returns the box model for the iframe. </p> <pre> $("p").html("This frame uses the W3C box model: &lt;span&gt;" + jQuery.support.boxModel + "&lt;/span&gt;"); </pre> <pre> &lt;p&gt; &lt;/p&gt; </pre> <p> Returns false if the page is in QuirksMode in Internet Explorer </p> <pre> jQuery.support.boxModel </pre> The number of elements currently matched. The &lt;a href='Core/size'&gt;size&lt;/a&gt; function will return the same value. <p> Count the divs. Click to add more. </p> <pre> $(document.body).click(function () { $(document.body).append($("&lt;div&gt;")); var n = $("div").length; $("span").text("There are " + n + " divs." + "Click to add more."); }).trigger('click'); // trigger the click to start </pre> <pre> &lt;span&gt;&lt;/span&gt; &lt;div&gt;&lt;/div&gt; </pre> &lt;p&gt;Should be used in conjunction with context to determine the exact query used. These two properties are mostly useful to plugin developers.&lt;/p&gt; <p> Determine the selector used. </p> <pre> $("ul") .append("&lt;li&gt;" + $("ul").selector + "&lt;/li&gt;") .append("&lt;li&gt;" + $("ul li").selector + "&lt;/li&gt;") .append("&lt;li&gt;" + $("div#foo ul:not([class])").selector + "&lt;/li&gt;"); </pre> <pre> Some selectors:&lt;ul&gt;&lt;/ul&gt; </pre> An expression whose matched elements are added for String, a string of HTML to create on the fly for DOMElement or one or more Elements to add if an Array. <p> Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow. </p> <pre> $("div").css("border", "2px solid red") .add("p") .css("background", "yellow"); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;p&gt;Added this... (notice no border)&lt;/p&gt; </pre> <p> Adds more elements, matched by the given expression, to the set of matched elements. </p> <pre> $("p").add("span").css("background", "yellow"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;span&gt;Hello Again&lt;/span&gt; </pre> <p> Adds more elements, created on the fly, to the set of matched elements. </p> <pre> $("p").clone().add("&lt;span&gt;Again&lt;/span&gt;").appendTo(document.body); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; </pre> <p> Adds one or more Elements to the set of matched elements. </p> <pre> $("p").add(document.getElementById("a")).css("background", "yellow"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;span id="a"&gt;Hello Again&lt;/span&gt; </pre> <p> Demonstrates how to add (or push) elements to an existing collection </p> <pre> var collection = $("p"); // capture the new collection collection = collection.add(document.getElementById("a")); collection.css("background", "yellow"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;span id="a"&gt;Hello Again&lt;/span&gt; </pre> One or more classes to add to the elements, these are separated by spaces. <p> Adds the class 'selected' to the matched elements. </p> <pre> $("p:last").addClass("selected"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;and&lt;/p&gt; &lt;p&gt;Goodbye&lt;/p&gt; </pre> <p> Adds the classes 'selected' and 'highlight' to the matched elements. </p> <pre> $("p:last").addClass("selected highlight"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;and&lt;/p&gt; &lt;p&gt;Goodbye&lt;/p&gt; </pre> Content to insert after each target. <p> Inserts some HTML after all paragraphs. </p> <pre> $("p").after("&lt;b&gt;Hello&lt;/b&gt;"); </pre> <pre> &lt;p&gt;I would like to say: &lt;/p&gt; </pre> <p> Inserts a DOM element after all paragraphs. </p> <pre> $("p").after( document.createTextNode("Hello") ); </pre> <pre> &lt;p&gt;I would like to say: &lt;/p&gt; </pre> <p> Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs. </p> <pre> $("p").after( $("b") ); </pre> <pre> &lt;b&gt;Hello&lt;/b&gt; &lt;p&gt;I would like to say: &lt;/p&gt; </pre> The XMLHttpRequest and settings used for that request are passed as arguments to the callback. The function to execute. &lt;pre&gt;function (event, XMLHttpRequest, ajaxOptions) { this; // dom element listening } &lt;/pre&gt; <p> Show a message when an AJAX request completes. </p> <pre> $("#msg").ajaxComplete(function(request, settings){ $(this).append("&lt;li&gt;Request Complete.&lt;/li&gt;"); }); </pre> The XMLHttpRequest and settings used for that request are passed as arguments to the callback. A third argument, an exception object, is passed if an exception occured while processing the request. The function to execute. &lt;pre&gt;function (event, XMLHttpRequest, ajaxOptions, thrownError) { // thrownError only passed if an error was caught this; // dom element listening } &lt;/pre&gt; <p> Show a message when an AJAX request fails. </p> <pre> $("#msg").ajaxError(function(event, request, settings){ $(this).append("&lt;li&gt;Error requesting page " + settings.url + "&lt;/li&gt;"); }); </pre> The XMLHttpRequest and settings used for that request are passed as arguments to the callback. The function to execute. &lt;pre&gt;function (event, XMLHttpRequest, ajaxOptions) { this; // dom element listening } &lt;/pre&gt; <p> Show a message before an AJAX request is sent. </p> <pre> $("#msg").ajaxSend(function(evt, request, settings){ $(this).append("&lt;li&gt;Starting request at " + settings.url + "&lt;/li&gt;"); }); </pre> The function to execute. &lt;pre&gt;function () { this; // dom element listening } &lt;/pre&gt; <p> Show a loading message whenever an AJAX request starts (and none is already active). </p> <pre> $("#loading").ajaxStart(function(){ $(this).show(); }); </pre> The function to execute. &lt;pre&gt;function () { this; // dom element listening } &lt;/pre&gt; <p> Hide a loading message after all the AJAX requests have stopped. </p> <pre> $("#loading").ajaxStop(function(){ $(this).hide(); }); </pre> The event object, XMLHttpRequest, and settings used for that request are passed as arguments to the callback. The function to execute. &lt;pre&gt;function (event, XMLHttpRequest, ajaxOptions) { this; // dom element listening } &lt;/pre&gt; <p> Show a message when an AJAX request completes successfully. </p> <pre> $("#msg").ajaxSuccess(function(evt, request, settings){ $(this).append("&lt;li&gt;Successful Request!&lt;/li&gt;"); }); </pre> Useful for traversing elements, and then adding something that was matched before the last traversal. <p> Find all divs, and all the paragraphs inside of them, and give them both classnames. Notice the div doesn't have the yellow background color since it didn't use andSelf(). </p> <pre> $("div").find("p").andSelf().addClass("border"); $("div").find("p").addClass("background"); </pre> <pre> &lt;div&gt; &lt;p&gt;First Paragraph&lt;/p&gt; &lt;p&gt;Second Paragraph&lt;/p&gt; &lt;/div&gt; </pre> The key aspect of this function is the object of style properties that will be animated, and to what end. Each key within the object represents a style property that will also be animated (e.g. "height", "top", or "opacity"). Note that properties should be specified using camel case, e.g. "marginLeft" instead of "margin-left." The value associated with the key represents to what end the property will be animated. If a number is provided as the value, then the style property will be transitioned from its current state to that new number. Otherwise if the string "hide", "show", or "toggle" is provided, a default animation will be constructed for that property. A set of style attributes that you wish to animate, and to what end. A set of options with which to configure the animation. <p> The first button shows how an unqueued animation works. It expands the div out to 90% width '''while''' the font-size is increasing. Once the font-size change is complete, the border animation will begin. The second button starts a traditional chained animation, where each animation will start once the previous animation on the element has completed. </p> <pre> $("#go1").click(function(){ $("#block1").animate( { width:"90%" }, { queue:false, duration:3000 } ) .animate( { fontSize:"24px" }, 1500 ) .animate( { borderRightWidth:"15px" }, 1500); }); $("#go2").click(function(){ $("#block2").animate( { width:"90%"}, 1000 ) .animate( { fontSize:"24px" } , 1000 ) .animate( { borderLeftWidth:"15px" }, 1000); }); $("#go3").click(function(){ $("#go1").add("#go2").click(); }); $("#go4").click(function(){ $("div").css({width:"", fontSize:"", borderWidth:""}); }); </pre> <pre> &lt;button id="go1"&gt;&raquo; Animate Block1&lt;/button&gt; &lt;button id="go2"&gt;&raquo; Animate Block2&lt;/button&gt; &lt;button id="go3"&gt;&raquo; Animate Both&lt;/button&gt; &lt;button id="go4"&gt;&raquo; Reset&lt;/button&gt; &lt;div id="block1"&gt;Block1&lt;/div&gt; &lt;div id="block2"&gt;Block2&lt;/div&gt; </pre> <p> Animates all paragraphs to toggle both height and opacity, completing the animation within 600 milliseconds. </p> <pre> $("p").animate({ "height": "toggle", "opacity": "toggle" }, { duration: "slow" }); </pre> <p> Animates all paragraph to a left style of 50 and opacity of 1 (opaque, visible), completing the animation within 500 milliseconds. It also will do it ''outside'' the queue, meaning it will automatically start without waiting for its turn. </p> <pre> $("p").animate({ left: "50px", opacity: 1 }, { duration: 500, queue: false }); </pre> <p> An example of using an 'easing' function to provide a different style of animation. This will only work if you have a plugin that provides this easing function. </p> <pre> $("p").animate({ "opacity": "show" }, { "duration": "slow", "easing": "easein" }); </pre> <p> An example of using a callback function. The first argument is an array of CSS properties, the second specifies that the animation should take 1000 milliseconds to complete, the third states the easing type, and the fourth argument is an anonymous callback function. </p> <pre> $("p").animate({ height:200, width:400, opacity: .5 }, 1000, "linear", function(){alert("all done");} ); </pre> The key aspect of this function is the object of style properties that will be animated, and to what end. Each key within the object represents a style property that will also be animated (for example: "height", "top", or "opacity"). Note that properties should be specified using camel case, e.g. "marginLeft" instead of "margin-left." The value associated with the key represents to what end the property will be animated. If a number is provided as the value, then the style property will be transitioned from its current state to that new number. Otherwise if the string "hide", "show", or "toggle" is provided, a default animation will be constructed for that property. Only properties that take numeric values are supported (e.g. backgroundColor is not supported). &lt;p&gt;As of jQuery 1.2 you can now animate properties by em and % (where applicable). Additionally, in jQuery 1.2, you can now do relative animations - specifying a "''+=''" or "''-=''" in front of the property value moves the element positively or negatively, relative to its current position.&lt;/p&gt; &lt;p&gt;As of jQuery 1.3 if you specify an animation duration of 0 then the animation will synchronously set the elements to their end state (this is different from old versions where there would be a brief, asynchronous, delay before the end state would be set).&lt;/p&gt; A set of style attributes that you wish to animate, and to what end. A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). The name of the easing effect that you want to use (plugin required). There are two built-in values, "linear" and "swing". A function to be executed whenever the animation completes, executes once for each element animated against. <p> Click the button to animate the div with a number of different properties. </p> <pre> // Using multiple unit types within one animation. $("#go").click(function(){ $("#block").animate({ width: "70%", opacity: 0.4, marginLeft: "0.6in", fontSize: "3em", borderWidth: "10px" }, 1500 ); }); </pre> <pre> &lt;button id="go"&gt;&raquo; Run&lt;/button&gt; &lt;div id="block"&gt;Hello!&lt;/div&gt; </pre> <p> Shows a div animate with a relative move. Click several times on the buttons to see the relative animations queued up. </p> <pre> $("#right").click(function(){ $(".block").animate({"left": "+=50px"}, "slow"); }); $("#left").click(function(){ $(".block").animate({"left": "-=50px"}, "slow"); }); </pre> <pre> &lt;button id="left"&gt;&laquo;&lt;/button&gt; &lt;button id="right"&gt;&raquo;&lt;/button&gt; &lt;div class="block"&gt;&lt;/div&gt; </pre> <p> Animates all paragraphs to toggle both height and opacity, completing the animation within 600 milliseconds. </p> <pre> $("p").animate({ "height": "toggle", "opacity": "toggle" }, "slow"); </pre> <p> Animates all paragraph to a left style of 50 and opacity of 1 (opaque, visible), completing the animation within 500 milliseconds. </p> <pre> $("p").animate({ "left": "50", "opacity": 1 }, 500); </pre> <p> An example of using an 'easing' function to provide a different style of animation. This will only work if you have a plugin that provides this easing function. Note, this code will do nothing unless the paragraph element is hidden. </p> <pre> $("p").animate({ "opacity": "show" }, "slow", "easein"); </pre> This operation is similar to doing an appendChild to all the specified elements, adding them into the document. Content to append to the target. <p> Appends some HTML to all paragraphs. </p> <pre> $("p").append("&lt;strong&gt;Hello&lt;/strong&gt;"); </pre> <pre> &lt;p&gt;I would like to say: &lt;/p&gt; </pre> <p> Appends an Element to all paragraphs. </p> <pre> $("p").append(document.createTextNode("Hello")); </pre> <pre> &lt;p&gt;I would like to say: &lt;/p&gt; </pre> <p> Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs. </p> <pre> $("p").append( $("strong") ); </pre> <pre> &lt;strong&gt;Hello&lt;/strong&gt;&lt;p&gt;I would like to say: &lt;/p&gt; </pre> This operation is, essentially, the reverse of doing a regular $(A).append(B), in that instead of appending B to A, you're appending A to B. target to which the content will be appended. <p> Appends all spans to the element with the ID "foo" </p> <pre> $("span").appendTo("#foo"); // check append() examples </pre> <pre> &lt;span&gt;I have nothing more to say... &lt;/span&gt; &lt;div id="foo"&gt;FOO! &lt;/div&gt; </pre> The name of the property to access. <p> Finds the title attribute of the first &lt;em&gt; in the page. </p> <pre> var title = $("em").attr("title"); $("div").text(title); </pre> <pre> &lt;p&gt; Once there was a &lt;em title="huge, gigantic"&gt;large&lt;/em&gt; dinosaur... &lt;/p&gt; The title of the emphasis is:&lt;div&gt;&lt;/div&gt; </pre> This serves as the best way to set a large number of properties on all matched elements. Note that you must use 'className' as key if you want to set the class-Attribute. Or use .addClass( class ) or .removeClass( class ). Keep in mind this recursively calls attr( key, value ) or attr ( key, fn ), so if one of the properties you are passing is a function, the function will be evaluated and not stored as the attribute itself. Key/value pairs to set as object properties. <p> Set some attributes for all &lt;img&gt;s in the page. </p> <pre> $("img").attr({ src: "/images/hat.gif", title: "jQuery", alt: "jQuery Logo" }); $("div").text($("img").attr("alt")); </pre> <pre> &lt;img /&gt; &lt;img /&gt; &lt;img /&gt; &lt;div&gt;&lt;B&gt;Attribute of Ajax&lt;/B&gt;&lt;/div&gt; </pre> The name of the property to set. The value to set the property to. <p> Disables buttons greater than the 1st button. </p> <pre> $("button:gt(1)").attr("disabled","disabled"); </pre> <pre> &lt;button&gt;0th Button&lt;/button&gt; &lt;button&gt;1st Button&lt;/button&gt; &lt;button&gt;2nd Button&lt;/button&gt; </pre> Instead of supplying a string value as described &lt;a href='#keyvalue'&gt;above&lt;/a&gt;, a function is provided that computes the value. The name of the property to set. A function returning the value to set. Scope: Current element, argument: Index of current element &lt;pre&gt;function callback(indexArray) { // indexArray == position in the jQuery object this; // dom element } &lt;/pre&gt; <p> Sets id for divs based on the position in the page. </p> <pre> $("div").attr("id", function (arr) { return "div-id" + arr; }) .each(function () { $("span", this).html("(ID = '&lt;b&gt;" + this.id + "&lt;/b&gt;')"); }); </pre> <pre> &lt;div&gt;Zero-th &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;First &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;Second &lt;span&gt;&lt;/span&gt;&lt;/div&gt; </pre> <p> Sets src attribute from title attribute on the image. </p> <pre> $("img").attr("src", function() { return "/images/" + this.title; }); </pre> <pre> &lt;img title="hat.gif"/&gt; &lt;img title="hat-old.gif"/&gt; &lt;img title="hat2-old.gif"/&gt; </pre> Content to insert before each target. <p> Inserts some HTML before all paragraphs. </p> <pre> $("p").before("&lt;b&gt;Hello&lt;/b&gt;"); </pre> <pre> &lt;p&gt; is what I said...&lt;/p&gt; </pre> <p> Inserts a DOM element before all paragraphs. </p> <pre> $("p").before( document.createTextNode("Hello") ); </pre> <pre> &lt;p&gt; is what I said...&lt;/p&gt; </pre> <p> Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs. </p> <pre> $("p").before( $("b") ); </pre> <pre> &lt;p&gt; is what I said...&lt;/p&gt;&lt;b&gt;Hello&lt;/b&gt; </pre> '''Possible event values:''' &lt;code&gt;blur&lt;/code&gt;, &lt;code&gt;focus&lt;/code&gt;, &lt;code&gt;load&lt;/code&gt;, &lt;code&gt;resize&lt;/code&gt;, &lt;code&gt;scroll&lt;/code&gt;, &lt;code&gt;unload&lt;/code&gt;, &lt;code&gt;beforeunload&lt;/code&gt;, &lt;code&gt;click&lt;/code&gt;, &lt;code&gt;dblclick&lt;/code&gt;, &lt;code&gt; mousedown&lt;/code&gt;, &lt;code&gt;mouseup&lt;/code&gt;, &lt;code&gt;mousemove&lt;/code&gt;, &lt;code&gt;mouseover&lt;/code&gt;, &lt;code&gt;mouseout&lt;/code&gt;, &lt;code&gt;mouseenter&lt;/code&gt;, &lt;code&gt;mouseleave&lt;/code&gt;, &lt;code&gt;change&lt;/code&gt;, &lt;code&gt;select&lt;/code&gt;, &lt;code&gt; submit&lt;/code&gt;, &lt;code&gt;keydown&lt;/code&gt;, &lt;code&gt;keypress&lt;/code&gt;, &lt;code&gt;keyup&lt;/code&gt;, &lt;code&gt;error&lt;/code&gt; The event handler is passed an &lt;a href='Events/jQuery.Event'&gt;event object&lt;/a&gt; that you can use to prevent default behaviour. To stop both default action and event bubbling, your handler has to return false. Note that this will prevent handlers on parent elements from running but not other jQuery handlers on the same element. The full list of properties that are available on the event object can be found in the &lt;a href='Events/jQuery.Event'&gt;jQuery.Event&lt;/a&gt; documentation. &lt;p&gt;In most cases, you can define your event handlers as anonymous functions (see first example). In cases where that is not possible, you can pass additional data as the second parameter (and the handler function as the third), see second example.&lt;/p&gt; &lt;p&gt;jQuery also supports &lt;a href='Namespaced_Events'&gt;namespaced events&lt;/a&gt;. These allow you to trigger or unbind specific groups of bound handlers without having to reference them directly.&lt;/p&gt; One or more event types separated by a space Additional data passed to the event handler as event.data A function to bind to the event on each of the set of matched elements, passed an &lt;a href='Events/jQuery.Event'&gt;event object&lt;/a&gt;. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Handle click and double-click for the paragraph. Note: the coordinates are window relative so in this case relative to the demo iframe. </p> <pre> $("p").bind("click", function(&lt;a href='Events/jQuery.Event'&gt;e&lt;/a&gt;){ var str = "( " + e.pageX + ", " + e.pageY + " )"; $("span").text("Click happened! " + str); }); $("p").bind("dblclick", function(){ $("span").text("Double-click happened in " + this.tagName); }); $("p").bind("mouseenter mouseleave", function(e){ $(this).toggleClass("over"); }); </pre> <pre> &lt;p&gt;Click or double click here.&lt;/p&gt; &lt;span&gt;&lt;/span&gt; </pre> <p> To display each paragraph's text in an alert box whenever it is clicked: </p> <pre> $("p").bind("click", function(){ alert( $(this).text() ); }); </pre> <p> You can pass some extra data before the event handler: </p> <pre> function handler(event) { alert(event.data.foo); } $("p").bind("click", {foo: "bar"}, handler) </pre> <p> To cancel a default action and prevent it from bubbling up, return false: </p> <pre> $("form").bind("submit", function() { return false; }) </pre> <p> To cancel only the default action by using the preventDefault method. </p> <pre> $("form").bind("submit", function(event){ event.preventDefault(); }); </pre> <p> Stop only an event from bubbling by using the stopPropagation method. </p> <pre> $("form").bind("submit", function(event){ event.stopPropagation(); }); </pre> <p> Can bind custom events too. </p> <pre> $("p").bind("myCustomEvent", function(e, myName, myValue){ $(this).text(myName + ", hi there!"); $("span").stop().css("opacity", 1) .text("myName = " + myName) .fadeIn(30).fadeOut(1000); }); $("button").click(function () { $("p").trigger("myCustomEvent", [ "John" ]); }); </pre> <pre> &lt;p&gt;Has an attached custom event.&lt;/p&gt; &lt;button&gt;Trigger custom event&lt;/button&gt; &lt;span style="display:none;"&gt;&lt;/span&gt; </pre> This causes all of the functions that have been bound to that blur event to be executed, and calls the browser's default blur action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the blur event. The blur event usually fires when an element loses focus either via the pointing device or by tabbing navigation <p> To triggers the blur event on all paragraphs: </p> <pre> $("p").blur(); </pre> The blur event fires when an element loses focus either via the pointing device or by tabbing navigation. A function to bind to the blur event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Fire blur. </p> <pre> $("input").blur(function () { $(this).next("span").css('display','inline').fadeOut(1000); }); </pre> <pre> &lt;p&gt;&lt;input type="text" /&gt; &lt;span&gt;blur fire&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="password" /&gt; &lt;span&gt;blur fire&lt;/span&gt;&lt;/p&gt; </pre> This causes all of the functions that have been bound to that change event to be executed, and calls the browser's default change action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the change event. The change event usually fires when a control loses the input focus and its value has been modified since gaining focus. The change event fires when a control loses the input focus and its value has been modified since gaining focus. A function to bind to the change event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Attaches a change event to the select that gets the text for each selected option and writes them in the div. It then triggers the event for the initial text draw. </p> <pre> $("select").change(function () { var str = ""; $("select option:selected").each(function () { str += $(this).text() + " "; }); $("div").text(str); }) .change(); </pre> <pre> &lt;select name="sweets" multiple="multiple"&gt; &lt;option&gt;Chocolate&lt;/option&gt; &lt;option selected="selected"&gt;Candy&lt;/option&gt; &lt;option&gt;Taffy&lt;/option&gt; &lt;option selected="selected"&gt;Caramel&lt;/option&gt; &lt;option&gt;Fudge&lt;/option&gt; &lt;option&gt;Cookie&lt;/option&gt; &lt;/select&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> To add a validity test to all text input elements: </p> <pre> $("input[@type='text']").change( function() { // check input ($(this).val()) for validity here }); </pre> This set can be filtered with an optional expression that will cause only elements matching the selector to be collected. Also note: while parents() will look at all ancestors, children() will only consider immediate child elements. An expression to filter the child Elements with. <p> Find all children of the clicked element. </p> <pre> $("#container").click(function (e) { $("*").removeClass("hilite"); var $kids = $(e.target).children(); var len = $kids.addClass("hilite").length; $("#results span:first").text(len); $("#results span:last").text(e.target.tagName); e.preventDefault(); return false; }); </pre> <pre> &lt;div id="container"&gt; &lt;div&gt; &lt;p&gt;This &lt;span&gt;is the &lt;em&gt;way&lt;/em&gt; we&lt;/span&gt; write &lt;em&gt;the&lt;/em&gt; demo,&lt;/p&gt; &lt;/div&gt; &lt;div&gt; &lt;a href="#"&gt;&lt;b&gt;w&lt;/b&gt;rit&lt;b&gt;e&lt;/b&gt;&lt;/a&gt; the &lt;span&gt;demo,&lt;/span&gt; &lt;button&gt;write the&lt;/button&gt; demo, &lt;/div&gt; &lt;div&gt; This &lt;span&gt;the way we &lt;em&gt;write&lt;/em&gt; the &lt;em&gt;demo&lt;/em&gt; so&lt;/span&gt; &lt;input type="text" value="early" /&gt; in &lt;/div&gt; &lt;p&gt; &lt;span&gt;t&lt;/span&gt;he &lt;span&gt;m&lt;/span&gt;orning. &lt;span id="results"&gt;Found &lt;span&gt;0&lt;/span&gt; children in &lt;span&gt;TAG&lt;/span&gt;.&lt;/span&gt; &lt;/p&gt; &lt;/div&gt; </pre> <p> Find all children of each div. </p> <pre> $("div").children().css("border-bottom", "3px double red"); </pre> <pre> &lt;p&gt;Hello (this is a paragraph)&lt;/p&gt; &lt;div&gt;&lt;span&gt;Hello Again (this span is a child of the a div)&lt;/span&gt;&lt;/div&gt; &lt;p&gt;And &lt;span&gt;Again&lt;/span&gt; (in another paragraph)&lt;/p&gt; &lt;div&gt;And One Last &lt;span&gt;Time&lt;/span&gt; (most text directly in a div)&lt;/div&gt; </pre> <p> Find all children with a class "selected" of each div. </p> <pre> $("div").children(".selected").css("color", "blue"); </pre> <pre> &lt;div&gt; &lt;span&gt;Hello&lt;/span&gt; &lt;p class="selected"&gt;Hello Again&lt;/p&gt; &lt;div class="selected"&gt;And Again&lt;/div&gt; &lt;p&gt;And One Last Time&lt;/p&gt; &lt;/div&gt; </pre> Causes all of the functions that have been bound to that click event to be executed. <p> To trigger the click event on all of the paragraphs on the page: </p> <pre> $("p").click(); </pre> The click event fires when the pointing device button is clicked over an element. A click is defined as a mousedown and mouseup over the same screen location. The sequence of these events is:&lt;ul&gt;&lt;li&gt;mousedown&lt;/li&gt;&lt;li&gt;mouseup&lt;/li&gt;&lt;li&gt;click&lt;/li&gt;&lt;/ul&gt; A function to bind to the click event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To hide paragraphs on a page when they are clicked: </p> <pre> $("p").click(function () { $(this).slideUp(); }); $("p").hover(function () { $(this).addClass("hilite"); }, function () { $(this).removeClass("hilite"); }); </pre> <pre> &lt;p&gt;First Paragraph&lt;/p&gt; &lt;p&gt;Second Paragraph&lt;/p&gt; &lt;p&gt;Yet one more Paragraph&lt;/p&gt; </pre> This is useful for moving copies of the elements to another location in the DOM. <p> Clones all b elements (and selects the clones) and prepends them to all paragraphs. </p> <pre> $("b").clone().prependTo("p"); </pre> <pre> &lt;b&gt;Hello&lt;/b&gt;&lt;p&gt;, how are you?&lt;/p&gt; </pre> This is useful for moving copies of the elements, and their events, to another location in the DOM. Set to true to enable cloning of event handlers. <p> Create a button that's able to clone itself - and have the clones themselves be clonable. </p> <pre> $("button").click(function(){ $(this).clone(true).insertAfter(this); }); </pre> <pre> &lt;button&gt;Clone Me!&lt;/button&gt; </pre> &lt;p&gt;Closest works by first looking at the current element to see if it matches the specified expression, if so it just returns the element itself. If it doesn't match then it will continue to traverse up the document, parent by parent, until an element is found that matches the specified expression. If no matching element is found then none will be returned.&lt;/p&gt; &lt;p&gt;Closest is especially useful for dealing with event delegation.&lt;/p&gt; An expression to filter the elements with. <p> Show how event delegation can be done with closest. </p> <pre> $(document).bind("click", function (e) { $(e.target).closest("li").toggleClass("hilight"); }); </pre> <pre> &lt;ul&gt; &lt;li&gt;&lt;b&gt;Click me!&lt;/b&gt;&lt;/li&gt; &lt;li&gt;You can also &lt;b&gt;Click me!&lt;/b&gt;&lt;/li&gt; &lt;/ul&gt; </pre> <p> Find all the text nodes inside a paragraph and wrap them with a bold tag. </p> <pre> $("p").contents().not("[nodeType=1]").wrap("&lt;b/&gt;"); </pre> <pre> &lt;p&gt;Hello &lt;a href="http://ejohn.org/"&gt;John&lt;/a&gt;, how are you doing?&lt;/p&gt; </pre> <p> Append some new content into an empty iframe. </p> <pre> $("iframe").contents().find("body").append("I'm in an iframe!"); </pre> <pre> &lt;iframe src="/index-blank.html" width="300" height="100"&gt;&lt;/iframe&gt; </pre> The name of the property to access. <p> To access the background color of a clicked div. </p> <pre> $("div").click(function () { var color = $(this).css("background-color"); $("#result").html("That div is &lt;span style='color:" + color + ";'&gt;" + color + "&lt;/span&gt;."); }); </pre> <pre> &lt;span id="result"&gt;&nbsp;&lt;/span&gt; &lt;div style="background-color:blue;"&gt;&lt;/div&gt; &lt;div style="background-color:rgb(15,99,30);"&gt;&lt;/div&gt; &lt;div style="background-color:#123456;"&gt;&lt;/div&gt; &lt;div style="background-color:#f11;"&gt;&lt;/div&gt; </pre> This is the best way to set several style properties on all matched elements. Key/value pairs to set as style properties. <p> To set the color of all paragraphs to red and background to blue: </p> <pre> $("p").hover(function () { $(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'}); }, function () { var cssObj = { 'background-color' : '#ddd', 'font-weight' : '', 'color' : 'rgb(0,40,244)' } $(this).css(cssObj); }); </pre> <pre> &lt;p&gt; Move the mouse over a paragraph. &lt;/p&gt; &lt;p&gt; Like this one or the one above. &lt;/p&gt; </pre> <p> If the property name includes a "-", put it between quotation marks: </p> <pre> $("p").hover(function () { $(this).css({ "background-color":"yellow", "font-weight":"bolder" }); }, function () { var cssObj = { "background-color": "#ddd", "font-weight": "", color: "rgb(0,40,244)" } $(this).css(cssObj); }); </pre> <pre> &lt;p&gt; Move the mouse over a paragraph. &lt;/p&gt; &lt;p&gt; Like this one or the one above. &lt;/p&gt; </pre> If a number is provided, it is automatically converted into a pixel value. The name of the property to set. The value to set the property to. <p> To change the color of any paragraph to red on mouseover event. </p> <pre> $("p").mouseover(function () { $(this).css("color","red"); }); </pre> <pre> &lt;p&gt; Just roll the mouse over me. &lt;/p&gt; &lt;p&gt; Or me to see a color change. &lt;/p&gt; </pre> <p> To highlight a clicked word in the paragraph. </p> <pre> var words = $("p:first").text().split(" "); var text = words.join("&lt;/span&gt; &lt;span&gt;"); $("p:first").html("&lt;span&gt;" + text + "&lt;/span&gt;"); $("span").click(function () { $(this).css("background-color","yellow"); }); </pre> <pre> &lt;p&gt; Once upon a time there was a man who lived in a pizza parlor. This man just loved pizza and ate it all the time. He went on to be the happiest man in the world. The end. &lt;/p&gt; </pre> &lt;p&gt;If the jQuery collection references multiple elements, the value returned refers to the first element.&lt;/p&gt;&lt;p&gt;This function is used to get stored data on an element without the risk of a circular reference. It uses jQuery.data and is new to version 1.2.3. It can be used for many reasons and jQuery UI uses it heavily. &lt;/p&gt; Name of the data stored. <p> Get the data named "blah" stored at for an element. </p> <pre> $("button").click(function(e) { var value; switch ($("button").index(this)) { case 0 : value = $("div").data("blah"); break; case 1 : $("div").data("blah", "hello"); value = "Stored!"; break; case 2 : $("div").data("blah", 86); value = "Stored!"; break; case 3 : $("div").removeData("blah"); value = "Removed!"; break; } $("span").text("" + value); }); </pre> <pre> &lt;div&gt;A div&lt;/div&gt; &lt;button&gt;Get "blah" from the div&lt;/button&gt; &lt;button&gt;Set "blah" to "hello"&lt;/button&gt; &lt;button&gt;Set "blah" to 86&lt;/button&gt; &lt;button&gt;Remove "blah" from the div&lt;/button&gt; &lt;p&gt;The "blah" value of this div is &lt;span&gt;?&lt;/span&gt;&lt;/p&gt; </pre> &lt;p&gt;If the jQuery collection references multiple elements, the data element is set on all of them.&lt;/p&gt;&lt;p&gt;This function can be useful for attaching data to elements without having to create a new expando. It also isn't limited to a string. The value can be any format.&lt;/p&gt;&lt;p&gt;It may also be used for getting events attached to elements, however this is unsupported. First paramater being the element, second being the string "events"&lt;/p&gt; Name of the data to store. Value to be stored. <p> Store then retrieve a value from the div element. </p> <pre> $("div").data("test", { first: 16, last: "pizza!" }); $("span:first").text($("div").data("test").first); $("span:last").text($("div").data("test").last); </pre> <pre> &lt;div&gt; The values stored were &lt;span&gt;&lt;/span&gt; and &lt;span&gt;&lt;/span&gt; &lt;/div&gt; </pre> This causes all of the functions that have been bound to that dblclick event to be executed, and calls the browser's default dblclick action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the dblclick event. The dblclick event usually fires when the pointing device button is double clicked over an element. The dblclick event fires when the pointing device button is double clicked over an element The function to bind to the dblclick event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To bind a "Hello World!" alert box the dblclick event on every paragraph on the page: </p> <pre> $("p").dblclick( function () { alert("Hello World!"); }); </pre> <p> Double click to toggle background color. </p> <pre> var divdbl = $("div:first"); divdbl.dblclick(function () { divdbl.toggleClass('dbl'); }); </pre> <pre> &lt;div&gt;&lt;/div&gt;&lt;span&gt;Double click the block&lt;/span&gt; </pre> The queue name (fx by default) <p> Use dequeue to end a custom queue function which allows the queue to keep going. </p> <pre> $("button").click(function () { $("div").animate({left:'+=200px'}, 2000); $("div").animate({top:'0px'}, 600); $("div").queue(function () { $(this).toggleClass("red"); $(this).dequeue(); }); $("div").animate({left:'10px', top:'30px'}, 700); }); </pre> <pre> &lt;button&gt;Start&lt;/button&gt; &lt;div&gt;&lt;/div&gt; </pre> &lt;p&gt;Without any arguments, all bound live events are removed.&lt;/p&gt;&lt;p&gt;You can also unbind custom events registered with live.&lt;/p&gt;&lt;p&gt;If the type is provided, all bound live events of that type are removed.&lt;/p&gt;&lt;p&gt;If the function that was passed to live is provided as the second argument, only that specific event handler is removed.&lt;/p&gt; A live event type to unbind. A function to unbind from the event on each of the set of matched elements. <p> Can bind and unbind events to the colored button. </p> <pre> function aClick() { $("div").show().fadeOut("slow"); } $("#bind").click(function () { $("#theone").live("click", aClick) .text("Can Click!"); }); $("#unbind").click(function () { $("#theone").die("click", aClick) .text("Does nothing..."); }); </pre> <pre> &lt;button id="theone"&gt;Does nothing...&lt;/button&gt; &lt;button id="bind"&gt;Bind Click&lt;/button&gt; &lt;button id="unbind"&gt;Unbind Click&lt;/button&gt; &lt;div style="display:none;"&gt;Click!&lt;/div&gt; </pre> <p> To unbind all live events from all paragraphs, write: </p> <pre> $("p").die() </pre> <p> To unbind all live click events from all paragraphs, write: </p> <pre> $("p").die( "click" ) </pre> <p> To unbind just one previously bound handler, pass the function in as the second argument: </p> <pre> var foo = function () { // code to handle some kind of event }; $("p").live("click", foo); // ... now foo will be called when paragraphs are clicked ... $("p").die("click", foo); // ... foo will no longer be called. </pre> This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points to the specific DOM element. Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set (integer, zero-index). Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop). The callback to execute for each matched element. &lt;pre&gt;function callback(index, domElement) { this; // this == domElement } &lt;/pre&gt; <p> Iterates over three divs and sets their color property. </p> <pre> $(document.body).click(function () { $("div").each(function (i) { if (this.style.color != "blue") { this.style.color = "blue"; } else { this.style.color = ""; } }); }); </pre> <pre> &lt;div&gt;Click here&lt;/div&gt; &lt;div&gt;to iterate through&lt;/div&gt; &lt;div&gt;these divs.&lt;/div&gt; </pre> <p> If you want to have the jQuery object instead of the regular DOM element, use the $(this) function, for example: </p> <pre> $("span").click(function () { $("li").each(function(){ $(this).toggleClass("example"); }); }); </pre> <pre> To do list: &lt;span&gt;(click here to change)&lt;/span&gt; &lt;ul&gt; &lt;li&gt;Eat&lt;/li&gt; &lt;li&gt;Sleep&lt;/li&gt; &lt;li&gt;Be merry&lt;/li&gt; &lt;/ul&gt; </pre> <p> You can use 'return' to break out of each() loops early. </p> <pre> $("button").click(function () { $("div").each(function (index, domEle) { // domEle == this $(domEle).css("backgroundColor", "yellow"); if ($(this).is("#stop")) { $("span").text("Stopped at div index #" + index); return false; } }); }); </pre> <pre> &lt;button&gt;Change colors&lt;/button&gt; &lt;span&gt;&lt;/span&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div id="stop"&gt;Stop here&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> Note that this function starting with 1.2.2 will also remove all event handlers and internally cached data. <p> Removes all child nodes (including text nodes) from all paragraphs </p> <pre> $("button").click(function () { $("p").empty(); }); </pre> <pre> &lt;p&gt; Hello, &lt;span&gt;Person&lt;/span&gt; &lt;a href="javascript:;"&gt;and person&lt;/a&gt; &lt;/p&gt; &lt;button&gt;Call empty() on above paragraph&lt;/button&gt; </pre> If there was no destructive operation before, an empty set is returned. A 'destructive' operation is any operation that changes the set of matched jQuery elements, which means any Traversing function that returns a jQuery object - including &lt;a href='Traversing/add'&gt;add&lt;/a&gt;, &lt;a href='Traversing/andSelf'&gt;andSelf&lt;/a&gt;, &lt;a href='Traversing/children'&gt;children&lt;/a&gt;, &lt;a href='Traversing/filter'&gt;filter&lt;/a&gt;, &lt;a href='Traversing/find'&gt;find&lt;/a&gt;, &lt;a href='Traversing/map'&gt;map&lt;/a&gt;, &lt;a href='Traversing/next'&gt;next&lt;/a&gt;, &lt;a href='Traversing/nextAll'&gt;nextAll&lt;/a&gt;, &lt;a href='Traversing/not'&gt;not&lt;/a&gt;, &lt;a href='Traversing/parent'&gt;parent&lt;/a&gt;, &lt;a href='Traversing/parents'&gt;parents&lt;/a&gt;, &lt;a href='Traversing/prev'&gt;prev&lt;/a&gt;, &lt;a href='Traversing/prevAll'&gt;prevAll&lt;/a&gt;, &lt;a href='Traversing/siblings'&gt;siblings&lt;/a&gt; and &lt;a href='Traversing/slice'&gt;slice&lt;/a&gt; - plus the &lt;a href='Manipulation/clone'&gt;clone&lt;/a&gt; function (from Manipulation). <p> Selects all paragraphs, finds span elements inside these, and reverts the selection back to the paragraphs. </p> <pre> jQuery.fn.showTags = function (n) { var tags = this.map(function () { return this.tagName; }) .get().join(", "); $("b:eq(" + n + ")").text(tags); return this; }; $("p").showTags(0) .find("span") .showTags(1) .css("background", "yellow") .end() .showTags(2) .css("font-style", "italic"); </pre> <pre> &lt;p&gt; Hi there &lt;span&gt;how&lt;/span&gt; are you &lt;span&gt;doing&lt;/span&gt;? &lt;/p&gt; &lt;p&gt; This &lt;span&gt;span&lt;/span&gt; is one of several &lt;span&gt;spans&lt;/span&gt; in this &lt;span&gt;sentence&lt;/span&gt;. &lt;/p&gt; &lt;div&gt; Tags in jQuery object initially: &lt;b&gt;&lt;/b&gt; &lt;/div&gt; &lt;div&gt; Tags in jQuery object after find: &lt;b&gt;&lt;/b&gt; &lt;/div&gt; &lt;div&gt; Tags in jQuery object after end: &lt;b&gt;&lt;/b&gt; &lt;/div&gt; </pre> <p> Selects all paragraphs, finds span elements inside these, and reverts the selection back to the paragraphs. </p> <pre> $("p").find("span").end().css("border", "2px red solid"); </pre> <pre> &lt;p&gt;&lt;span&gt;Hello&lt;/span&gt;, how are you?&lt;/p&gt; </pre> The position of the element in the set of matched elements starts at 0 and goes to length - 1. The index of the element to select. <p> Reduces the selection to the second selected element. </p> <pre> $("p").eq(1).css("color", "red") </pre> <pre> &lt;p&gt;This is just a test.&lt;/p&gt;&lt;p&gt;So is this&lt;/p&gt; </pre> Argument is the position of the element in the set of matched elements, starting at 0 and going to length - 1. Since the query filters out all elements that do not match the given index, providing an invalid index returns an empty set of elements rather than null. The index of the element in the jQuery object. <p> Turn the div with index 2 blue by adding an appropriate class. </p> <pre> $("div").eq(2).addClass("blue"); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> This causes all of the functions that have been bound to that error event to be executed, and calls the browser's default error action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the error event. The error event usually fires when an element loses focus either via the pointing device or by tabbing navigation. &lt;p&gt;There is no public standard for the error event. In most browsers, the window object's error event is triggered when a JavaScript error occurs on the page. An image object's error event is triggered when it is set an invalid src attribute - either a non-existent file, or one with corrupt image data.&lt;/p&gt;&lt;p&gt;If the event is thrown by the window object, the event handler will be passed three parameters: &lt;ol&gt;&lt;li&gt;A message describing the event ("varName is not defined", "missing operator in expression", etc.),&lt;/li&gt;&lt;li&gt;the full URL of the document containing the error, and&lt;/li&gt;&lt;li&gt;the line number on which the error occured.&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt; &lt;p&gt;If the event handler returns true, it signifies that the event was handled, and the browser raises no errors.&lt;/p&gt;&lt;p&gt;For more information, see: &lt;ul&gt;&lt;li&gt;[http://msdn2.microsoft.com/en-us/library/ms536930.aspx msdn - onerror Event]&lt;/li&gt;&lt;li&gt;[http://developer.mozilla.org/en/docs/DOM:window.onerror Gecko DOM Reference - onerror Event]&lt;/li&gt;&lt;li&gt;[http://developer.mozilla.org/en/docs/DOM:event Gecko DOM Reference - Event object]&lt;/li&gt;&lt;li&gt;[http://en.wikipedia.org/wiki/DOM_Events Wikipedia: DOM Events]&lt;/ul&gt;&lt;/p&gt; An event handler function to bind to the error event. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To keep a server-side log of JavaScript errors, you might want to: </p> <pre> $(window).error(function(msg, url, line){ jQuery.post("js_error_log.php", { msg: msg, url: url, line: line }); }); </pre> <p> To hide JavaScript errors from the user, you can try: </p> <pre> $(window).error(function(){ return true; }); </pre> <p> To hide the "broken image" icons for your IE users, you can try: </p> <pre> $("img").error(function(){ $(this).hide(); }); </pre> Only the opacity is adjusted for this animation, meaning that all of the matched elements should already have some form of height and width associated with them. A string representing one of the three predefined speeds ("slow", "def", or "fast") or the number of milliseconds to run the animation (e.g. 1000). As of jQuery 1.2.6, "normal" or any other string works the same as "def" (400ms). A function to be executed whenever the animation completes, executes once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates hidden divs to fade in one by one, completing each animation within 600 milliseconds. </p> <pre> $(document.body).click(function () { $("div:hidden:first").fadeIn("slow"); }); </pre> <pre> &lt;span&gt;Click here...&lt;/span&gt; &lt;div id="one"&gt;&lt;/div&gt; &lt;div id="two"&gt;&lt;/div&gt; &lt;div id="three"&gt;&lt;/div&gt; </pre> <p> Fades a red block in over the text. Once the animation is done, it quickly fades in more text on top. </p> <pre> $("a").click(function () { $("div").fadeIn(3000, function () { $("span").fadeIn(100); }); return false; }); </pre> <pre> &lt;p&gt; Let it be known that the party of the first part and the party of the second part are henceforth and hereto directed to assess the allegations for factual correctness... (&lt;a href="#"&gt;click!&lt;/a&gt;) &lt;div&gt;&lt;span&gt;CENSORED!&lt;/span&gt;&lt;/div&gt; &lt;/p&gt; </pre> Only the opacity is adjusted for this animation, meaning that all of the matched elements should already have some form of height and width associated with them. A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). A function to be executed whenever the animation completes, executes once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates all paragraphs to fade out, completing the animation within 600 milliseconds. </p> <pre> $("p").click(function () { $("p").fadeOut("slow"); }); </pre> <pre> &lt;p&gt; If you click on this paragraph you'll see it just fade away. &lt;/p&gt; </pre> <p> Fades out spans in one section that you click on. </p> <pre> $("span").click(function () { $(this).fadeOut(1000, function () { $("div").text("'" + $(this).text() + "' has faded!"); $(this).remove(); }); }); $("span").hover(function () { $(this).addClass("hilite"); }, function () { $(this).removeClass("hilite"); }); </pre> <pre> &lt;h3&gt;Find the modifiers - &lt;div&gt;&lt;/div&gt;&lt;/h3&gt; &lt;p&gt; If you &lt;span&gt;really&lt;/span&gt; want to go outside &lt;span&gt;in the cold&lt;/span&gt; then make sure to wear your &lt;span&gt;warm&lt;/span&gt; jacket given to you by your &lt;span&gt;favorite&lt;/span&gt; teacher. &lt;/p&gt; </pre> Only the opacity is adjusted for this animation, meaning that all of the matched elements should already have some form of height and width associated with them. A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). The opacity to fade to (a number from 0 to 1). A function to be executed whenever the animation completes, executed once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates first paragraph to fade to an opacity of 0.33 (33%, about one third visible), completing the animation within 600 milliseconds. </p> <pre> $("p:first").click(function () { $(this).fadeTo("slow", 0.33); }); </pre> <pre> &lt;p&gt; Click this paragraph to see it fade. &lt;/p&gt; &lt;p&gt; Compare to this one that won't fade. &lt;/p&gt; </pre> <p> Fade div to a random opacity on each click, completing the animation within 200 milliseconds. </p> <pre> $("div").click(function () { $(this).fadeTo("fast", Math.random()); }); </pre> <pre> &lt;p&gt;And this is the library that John built...&lt;/p&gt; &lt;div id="one"&gt;&lt;/div&gt; &lt;div id="two"&gt;&lt;/div&gt; &lt;div id="three"&gt;&lt;/div&gt; </pre> <p> Find the right answer! The fade will take 250 milliseconds and change various styles when it completes. </p> <pre> var getPos = function (n) { return (Math.floor(n) * 90) + "px"; }; $("p").each(function (n) { var r = Math.floor(Math.random() * 3); var tmp = $(this).text(); $(this).text($("p:eq(" + r + ")").text()); $("p:eq(" + r + ")").text(tmp); $(this).css("left", getPos(n)); }); $("div").each(function (n) { $(this).css("left", getPos(n)); }) .css("cursor", "pointer") .click(function () { $(this).fadeTo(250, 0.25, function () { $(this).css("cursor", "") .prev().css({"font-weight": "bolder", "font-style": "italic"}); }); }); </pre> <pre> &lt;p&gt;Wrong&lt;/p&gt; &lt;div&gt;&lt;/div&gt; &lt;p&gt;Wrong&lt;/p&gt; &lt;div&gt;&lt;/div&gt; &lt;p&gt;Right!&lt;/p&gt; &lt;div&gt;&lt;/div&gt; </pre> This method is used to narrow down the results of a search. Provide a comma-separated list of expressions to apply multiple filters at once. An expression to pass into the filter <p> Change the color of all divs then put a border around only some of them. </p> <pre> $("div").css("background", "#c8ebcc") .filter(".middle") .css("border-color", "red"); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div class="middle"&gt;&lt;/div&gt; &lt;div class="middle"&gt;&lt;/div&gt; &lt;div class="middle"&gt;&lt;/div&gt; &lt;div class="middle"&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Selects all paragraphs and removes those without a class "selected". </p> <pre> $("p").filter(".selected") </pre> <p> Selects all paragraphs and removes those that aren't of class "selected" or the first one. </p> <pre> $("p").filter(".selected, :first") </pre> The function is called with a context equal to the current element (just like &lt;a href='Core/each'&gt;$.each&lt;/a&gt;). If the function returns false, then the element is removed - anything else and the element is kept. A function to pass into the filter &lt;pre&gt;function callback(indexInJQueryObject) { var keepItBoolean = true; this; // dom element return keepItBoolean; } &lt;/pre&gt; <p> Change the color of all divs then put a border to specific ones. </p> <pre> $("div").css("background", "#b4b0da") .filter(function (index) { return index == 1 || $(this).attr("id") == "fourth"; }) .css("border", "3px double red"); </pre> <pre> &lt;div id="first"&gt;&lt;/div&gt; &lt;div id="second"&gt;&lt;/div&gt; &lt;div id="third"&gt;&lt;/div&gt; &lt;div id="fourth"&gt;&lt;/div&gt; &lt;div id="fifth"&gt;&lt;/div&gt; &lt;div id="sixth"&gt;&lt;/div&gt; </pre> <p> Remove all elements that have a descendant ol element </p> <pre> $("p").filter(function(index) { return $("ol", this).length == 0; }); </pre> All searching is done using a &lt;a href='Selectors'&gt;jQuery expression&lt;/a&gt;. The expression can be written using CSS 1-3 Selector syntax. An expression to search with. <p> Starts with all paragraphs and searches for descendant span elements, same as $("p span") </p> <pre> $("p").find("span").css('color','red'); </pre> <pre> &lt;p&gt;&lt;span&gt;Hello&lt;/span&gt;, how are you?&lt;/p&gt; &lt;p&gt;Me? I'm &lt;span&gt;good&lt;/span&gt;.&lt;/p&gt; </pre> <p> Add spans around each word then add a hover and italicize words with the letter '''t'''. </p> <pre> var newText = $("p").text().split(" ").join("&lt;/span&gt; &lt;span&gt;"); newText = "&lt;span&gt;" + newText + "&lt;/span&gt;"; $("p").html(newText) .find("span") .hover(function () { $(this).addClass("hilite"); }, function () { $(this).removeClass("hilite"); }) .end() .find(":contains('t')") .css({"font-style":"italic", "font-weight":"bolder"}); </pre> <pre> &lt;p&gt; When the day is short find that which matters to you or stop believing &lt;/p&gt; </pre> This causes all of the functions that have been bound to the focus event to be executed. Note that this does not execute the focus method of the underlying elements. <p> To focus on a login input box with id 'login' on page startup, try: </p> <pre> $(document).ready(function(){ $("#login").focus(); }); </pre> The focus event fires when an element receives focus either via the pointing device or by tab navigation. A function to bind to the focus event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Fire focus. </p> <pre> $("input").focus(function () { $(this).next("span").css('display','inline').fadeOut(1000); }); </pre> <pre> &lt;p&gt;&lt;input type="text" /&gt; &lt;span&gt;focus fire&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="password" /&gt; &lt;span&gt;focus fire&lt;/span&gt;&lt;/p&gt; </pre> <p> To stop people from writing in text input boxes, try: </p> <pre> $("input[@type=text]").focus(function(){ $(this).blur(); }); </pre> This serves as a backwards-compatible way of accessing all matched elements (other than the jQuery object itself, which is, in fact, an array of elements). It is useful if you need to operate on the DOM elements themselves instead of using built-in jQuery functions. <p> Selects all divs in the document and returns the DOM Elements as an Array, then uses the built-in reverse-method to reverse that array. </p> <pre> function disp(divs) { var a = []; for (var i = 0; i &lt; divs.length; i++) { a.push(divs[i].innerHTML); } $("span").text(a.join(" ")); } disp( $("div").get().reverse() ); </pre> <pre> Reversed - &lt;span&gt;&lt;/span&gt; &lt;div&gt;One&lt;/div&gt; &lt;div&gt;Two&lt;/div&gt; &lt;div&gt;Three&lt;/div&gt; </pre> This allows you to extract the actual DOM element and operate on it directly without necessarily using jQuery functionality on it. This function called as $(this).get(0) is the equivalent of using square bracket notation on the jQuery object itself like $(this)[0]. Access the element in the Nth position. <p> Gives the tag name of the element clicked on. </p> <pre> $("*", document.body).click(function (e) { e.stopPropagation(); var domEl = $(this).get(0); $("span:first").text("Clicked on - " + domEl.tagName); }); </pre> <pre> &lt;span&gt;&nbsp;&lt;/span&gt; &lt;p&gt;In this paragraph is an &lt;span&gt;important&lt;/span&gt; section&lt;/p&gt; &lt;div&gt;&lt;input type="text" /&gt;&lt;/div&gt; </pre> One CSS class name to be checked for. <p> Looks for the class 'selected' on the matched elements. </p> <pre> $("div#result1").append($("p:first").hasClass("selected").toString()); $("div#result2").append($("p:last").hasClass("selected").toString()); $("div#result3").append($("p").hasClass("selected").toString()); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p class="selected"&gt;Goodbye&lt;/p&gt; &lt;div id="result1"&gt;First paragraph has selected class: &lt;/div&gt; &lt;div id="result2"&gt;Last paragraph has selected class: &lt;/div&gt; &lt;div id="result3"&gt;Some paragraph has selected class: &lt;/div&gt; </pre> This is an alternative to is("." + class). The class to match. <p> Check to see if an element has a specific class, and if so, perform an action. </p> <pre> $("div").click(function(){ if ( $(this).hasClass("protected") ) $(this).animate({ left: -10 }, 75) .animate({ left: 10 }, 75) .animate({ left: -10 }, 75) .animate({ left: 10 }, 75) .animate({ left: 0 }, 75); }); </pre> <pre> &lt;span&gt;&lt;/span&gt;&lt;div class="protected"&gt;&lt;/div&gt; &lt;span&gt;&lt;/span&gt;&lt;div&gt;&lt;/div&gt; &lt;span&gt;&lt;/span&gt;&lt;div&gt;&lt;/div&gt; &lt;span&gt;&lt;/span&gt;&lt;div class="protected"&gt;&lt;/div&gt; </pre> In jQuery 1.2, this method is able to find the height of the window and document. <p> Show various heights. Note the values are from the iframe so might be smaller than you expected. The yellow highlight shows the iframe body. </p> <pre> function showHeight(ele, h) { $("div").text("The height for the " + ele + " is " + h + "px."); } $("#getp").click(function () { showHeight("paragraph", $("p").height()); }); $("#getd").click(function () { showHeight("document", $(document).height()); }); $("#getw").click(function () { showHeight("window", $(window).height()); }); </pre> <pre> &lt;button id="getp"&gt;Get Paragraph Height&lt;/button&gt; &lt;button id="getd"&gt;Get Document Height&lt;/button&gt; &lt;button id="getw"&gt;Get Window Height&lt;/button&gt; &lt;div&gt;&nbsp;&lt;/div&gt; &lt;p&gt; Sample paragraph to test height &lt;/p&gt; </pre> If no explicit unit was specified (like 'em' or '%') then "px" is concatenated to the value. Set the CSS 'height' property to the specified value. <p> To set the height of each div on click to 30px plus a color change. </p> <pre> $("div").one('click', function () { $(this).height(30) .css({cursor:"auto", backgroundColor:"green"}); }); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> Same as &lt;a href='Effects/hide#speedcallback'&gt;hide( speed, [callback] )&lt;/a&gt; without animations. Doesn't change anything if the selected elements are all hidden. <p> Hides all paragraphs then the link on click. </p> <pre> $("p").hide(); $("a").click(function () { $(this).hide(); return false; }); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;a href="#"&gt;Click to hide me too&lt;/a&gt; &lt;p&gt;Here is another paragraph&lt;/p&gt; </pre> &lt;p&gt;The height, width, and opacity of each of the matched elements are changed dynamically according to the specified speed. As of jQuery 1.3 the padding and margin are also animated, creating a smoother effect.&lt;/p&gt; A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). A function to be executed whenever the animation completes, executes once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates all shown paragraphs to hide slowly, completing the animation within 600 milliseconds. </p> <pre> $("button").click(function () { $("p").hide("slow"); }); </pre> <pre> &lt;button&gt;Hide 'em&lt;/button&gt; &lt;p&gt;Hiya&lt;/p&gt; &lt;p&gt;Such interesting text, eh?&lt;/p&gt; </pre> <p> Animates all spans (words in this case) to hide fastly, completing each animation within 200 milliseconds. Once each animation is done, it starts the next one. </p> <pre> $("#hidr").click(function () { $("span:last-child").hide("fast", function () { // use callee so don't have to name the function $(this).prev().hide("fast", arguments.callee); }); }); $("#showr").click(function () { $("span").show(2000); }); </pre> <pre> &lt;button id="hidr"&gt;Hide&lt;/button&gt; &lt;button id="showr"&gt;Show&lt;/button&gt; &lt;div&gt; &lt;span&gt;Once&lt;/span&gt; &lt;span&gt;upon&lt;/span&gt; &lt;span&gt;a&lt;/span&gt; &lt;span&gt;time&lt;/span&gt; &lt;span&gt;there&lt;/span&gt; &lt;span&gt;were&lt;/span&gt; &lt;span&gt;three&lt;/span&gt; &lt;span&gt;programmers...&lt;/span&gt; &lt;/div&gt; </pre> <p> Hides the divs when clicked over 2 seconds, then removes the div element when its hidden. Try clicking on more than one box at a time. </p> <pre> for (var i = 0; i &lt; 5; i++) { $("&lt;div&gt;").appendTo(document.body); } $("div").click(function () { $(this).hide(2000, function () { $(this).remove(); }); }); </pre> <pre> &lt;div&gt;&lt;/div&gt; </pre> Whenever the mouse cursor is moved over a matched element, the first specified function is fired. Whenever the mouse moves off of the element, the second specified function fires. Additionally, checks are in place to see if the mouse is still within the specified element itself (for example, an image inside of a div), and if it is, it will continue to 'hover', and not move out (a common error in using a mouseout event handler). The function to fire when the mouse is moved over a matched element. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; The function to fire when the mouse is moved off of a matched element. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To add a special style to list items that are being hovered over, try: </p> <pre> $("li").hover( function () { $(this).append($("&lt;span&gt; ***&lt;/span&gt;")); }, function () { $(this).find("span:last").remove(); } ); //Another example for mouse over effect on hyperlinks on page $("a").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);}); </pre> <pre> &lt;ul&gt; &lt;li&gt;Milk&lt;/li&gt; &lt;li&gt;Bread&lt;/li&gt; &lt;li&gt;&lt;a href='#'&gt;Chips&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href='#'&gt;Socks&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </pre> <p> To add a special style to table cells that are being hovered over, try: </p> <pre> $("td").hover( function () { $(this).addClass("hover"); }, function () { $(this).removeClass("hover"); } ); </pre> <p> To unbind the above example use: </p> <pre> $("td").unbind('mouseenter mouseleave'); </pre> <p> Click a paragraph to convert it from html to text. </p> <pre> $("p").click(function () { var htmlStr = $(this).html(); $(this).text(htmlStr); }); </pre> <pre> &lt;p&gt; &lt;b&gt;Click&lt;/b&gt; to change the &lt;span id="tag"&gt;html&lt;/span&gt; &lt;/p&gt; &lt;p&gt; to a &lt;span id="text"&gt;text&lt;/span&gt; node. &lt;/p&gt; &lt;p&gt; This &lt;button name="nada"&gt;button&lt;/button&gt; does nothing. &lt;/p&gt; </pre> <p> Click a paragraph to convert it from html to text. </p> <pre> $("p").click(function () { var htmlStr = $(this).html(); $(this).text(htmlStr); }); </pre> <pre> &lt;p&gt; &lt;b&gt;Click&lt;/b&gt; to change the &lt;span id="tag"&gt;html&lt;/span&gt; &lt;/p&gt; &lt;p&gt; to a &lt;span id="text"&gt;text&lt;/span&gt; node. &lt;/p&gt; &lt;p&gt; This &lt;button name="nada"&gt;button&lt;/button&gt; does nothing. &lt;/p&gt; </pre> Set the html contents to the specified value. <p> Add some html to each div. </p> <pre> $("div").html("&lt;span class='red'&gt;Hello &lt;b&gt;Again&lt;/b&gt;&lt;/span&gt;"); </pre> <pre> &lt;span&gt;Hello&lt;/span&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Add some html to each div then immediately do further manipulations to the inserted html. </p> <pre> $("div").html("&lt;b&gt;Wow!&lt;/b&gt; Such excitement..."); $("div b").append(document.createTextNode("!!!")) .css("color", "red"); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> Set the html contents to the specified value. <p> Add some html to each div. </p> <pre> $("div").html("&lt;span class='red'&gt;Hello &lt;b&gt;Again&lt;/b&gt;&lt;/span&gt;"); </pre> <pre> &lt;span&gt;Hello&lt;/span&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Add some html to each div then immediately do further manipulations to the inserted html. </p> <pre> $("div").html("&lt;b&gt;Wow!&lt;/b&gt; Such excitement..."); $("div b").append(document.createTextNode("!!!")) .css("color", "red"); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> Returns -1 if the object wasn't found. Object to search for. <p> On click, returns the index (based zero) of that div in the page. </p> <pre> $("div").click(function () { // this is the dom element clicked var index = $("div").index(this); $("span").text("That was div index #" + index); }); </pre> <pre> &lt;span&gt;Click a div!&lt;/span&gt; &lt;div&gt;First div&lt;/div&gt; &lt;div&gt;Second div&lt;/div&gt; &lt;div&gt;Third div&lt;/div&gt; </pre> <p> Returns the index for the element with ID foobar. </p> <pre> $("*").index( $('#foobar')[0] ) </pre> <pre> &lt;div id="foobar"&gt;&lt;b&gt;&lt;/b&gt;&lt;span id="foo"&gt;&lt;/span&gt;&lt;/div&gt; </pre> <p> Returns the index for the element with ID foo within another element. </p> <pre> $("*").index( $('#foo') ) </pre> <pre> &lt;div id="foobar"&gt;&lt;b&gt;&lt;/b&gt;&lt;span id="foo"&gt;&lt;/span&gt;&lt;/div&gt; </pre> <p> Returns the index for the element clicked within a collection. </p> <pre> var mainNavLinks = $('ul#mainNav li a'); mainNavLinks.click(function(){alert(mainNavLinks.index(this));}); </pre> <p> Returns -1, as there is no element with ID bar. </p> <pre> $("*").index( $('#bar')[0] ) </pre> <pre> &lt;div id="foobar"&gt;&lt;b&gt;&lt;/b&gt;&lt;span id="foo"&gt;&lt;/span&gt;&lt;/div&gt; </pre> This method works for both visible and hidden elements. <p> Get innerHeight </p> <pre> var p = $("p:first"); $("p:last").text( "innerHeight:" + p.innerHeight() ); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;p&gt;&lt;/p&gt; </pre> This method works for both visible and hidden elements. <p> Get innerWidth </p> <pre> var p = $("p:first"); $("p:last").text( "innerWidth:" + p.innerWidth() ); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;p&gt;&lt;/p&gt; </pre> This operation is, essentially, the reverse of doing a regular $(A).after(B), in that instead of inserting B after A, you're inserting A after B. Content after which the selected element(s) is inserted. <p> Inserts all paragraphs after an element with id of "foo". Same as $("#foo").after("p") </p> <pre> $("p").insertAfter("#foo"); // check after() examples </pre> <pre> &lt;p&gt; is what I said... &lt;/p&gt;&lt;div id="foo"&gt;FOO!&lt;/div&gt; </pre> This operation is, essentially, the reverse of doing a regular $(A).before(B), in that instead of inserting B before A, you're inserting A before B. Content after which the selected element(s) is inserted. <p> Inserts all paragraphs before an element with id of "foo". Same as $("#foo").before("p") </p> <pre> $("p").insertBefore("#foo"); // check before() examples </pre> <pre> &lt;div id="foo"&gt;FOO!&lt;/div&gt;&lt;p&gt;I would like to say: &lt;/p&gt; </pre> &lt;p&gt;If no element fits, or the expression is not valid, then the response will be 'false'.&lt;/p&gt; &lt;p&gt;'''Note''': As of jQuery 1.3 all expressions are supported. Previously complex expressions, such as those containing hierarchy selectors (such as +, ~, and &gt;), always returned 'true'.&lt;/p&gt; &lt;p&gt;&lt;a href='Traversing/filter'&gt;filter&lt;/a&gt; is used internally, therefore all rules that apply there apply here, as well.&lt;/p&gt; The expression with which to filter <p> Shows a few ways is() can be used inside an event handler. </p> <pre> $("div").one('click', function () { if ($(this).is(":first-child")) { $("p").text("It's the first div."); } else if ($(this).is(".blue,.red")) { $("p").text("It's a blue or red div."); } else if ($(this).is(":contains('Peter')")) { $("p").text("It's Peter!"); } else { $("p").html("It's nothing &lt;em&gt;special&lt;/em&gt;."); } $("p").hide().slideDown("slow"); $(this).css({"border-style": "inset", cursor:"default"}); }); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div class="blue"&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div class="red"&gt;&lt;/div&gt; &lt;div&gt;&lt;br/&gt;&lt;span&gt;Peter&lt;/span&gt;&lt;/div&gt; &lt;div class="blue"&gt;&lt;/div&gt; &lt;p&gt;&nbsp;&lt;/p&gt; </pre> <p> Returns true, because the parent of the input is a form element </p> <pre> var isFormParent = $("input[@type='checkbox']").parent().is("form") $("div").text("isFormParent = " + isFormParent); </pre> <pre> &lt;form&gt;&lt;input type="checkbox" /&gt;&lt;/form&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Returns false, because the parent of the input is a p element </p> <pre> var isFormParent = $("input[@type='checkbox']").parent().is("form") $("div").text("isFormParent = " + isFormParent); </pre> <pre> &lt;form&gt;&lt;p&gt;&lt;input type="checkbox" /&gt;&lt;/p&gt;&lt;/form&gt; &lt;div&gt;&lt;/div&gt; </pre> This is jQuery's low-level AJAX implementation. See $.get, $.post etc. for higher-level abstractions that are often easier to understand and use, but don't offer as much functionality (such as error callbacks). $.ajax() returns the XMLHttpRequest that it creates. In most cases you won't need that object to manipulate directly, but it is available if you need to abort the request manually. $.ajax() takes one argument, an object of key/value pairs, that are used to initialize and handle the request. See below for a full list of the key/values that can be used. '''Note:''' If you specify the dataType option described below, make sure the server sends the correct MIME type in the response (eg. xml as "text/xml"). Sending the wrong MIME type can lead to unexpected problems in your script. See &lt;a href='Specifying_the_Data_Type_for_AJAX_Requests'&gt;Specifying the Data Type for AJAX Requests&lt;/a&gt; for more information. '''Note:''' All remote (not on the same domain) POST requests are converted to GET when 'script' is the dataType (because it loads script using a DOM script tag). As of jQuery 1.2, you can load JSON data located on another domain if you specify a [http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/ JSONP] callback, which can be done like so: "myurl?callback=?". jQuery automatically replaces the ? with the correct method name to call, calling your specified callback. Or, if you set the dataType to "jsonp" a callback will be automatically added to your Ajax request. A set of key/value pairs that configure the Ajax request. All options are optional. A default can be set for any option with &lt;a href='Ajax/jQuery.ajaxSetup'&gt;$.ajaxSetup&lt;/a&gt;(). <p> Load and execute a JavaScript file. </p> <pre> $.ajax({ type: "GET", url: "test.js", dataType: "script" }); </pre> <p> Save some data to the server and notify the user once its complete. </p> <pre> $.ajax({ type: "POST", url: "some.php", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } }); </pre> <p> Retrieve the latest version of an HTML page. </p> <pre> $.ajax({ url: "test.html", cache: false, success: function(html){ $("#results").append(html); } }); </pre> <p> Loads data synchronously. Blocks the browser while the requests is active. It is better to block user interaction by other means when synchronization is necessary. </p> <pre> var html = $.ajax({ url: "some.php", async: false }).responseText; </pre> <p> Sends an xml document as data to the server. By setting the processData option to false, the automatic conversion of data to strings is prevented. </p> <pre> var xmlDocument = [create xml document]; $.ajax({ url: "page.php", processData: false, data: xmlDocument, success: handleResponse }); </pre> See &lt;a href='Ajax/jQuery.ajax'&gt;$.ajax&lt;/a&gt; for a description of all available options. A set of key/value pairs that configure the default Ajax request. All options are optional. <p> Sets the defaults for AJAX requests to the url "/xmlhttp/", disables global handlers and uses POST instead of GET. The following AJAX requests then sends some data without having to set anything else. </p> <pre> $.ajaxSetup({ url: "/xmlhttp/", global: false, type: "POST" }); $.ajax({ data: myData }); </pre> Typically this function will only be used internally. It is called automatically when necessary when using the other data() functionality. DOM element of interest. <p> Get the store id of an element. It is assigned on the data() function call if one hasn't been assigned yet. </p> <pre> $(document.body).click(function(e) { var id = jQuery.data(e.target); $("span").text(id); }); </pre> <pre> &lt;div&gt;A div&lt;/div&gt; &lt;div&gt;Another&lt;/div&gt; &lt;p&gt;The id of this div is &lt;span&gt;?&lt;/span&gt;&lt;/p&gt; </pre> DOM element of interest. Name of the data stored. <p> Get the data named "blah" stored at for an element. </p> <pre> $("button").click(function(e) { var adiv = $("div").get(0); var value; switch ($("button").index(this)) { case 0 : value = jQuery.data(adiv, "blah"); break; case 1 : jQuery.data(adiv, "blah", "hello"); value = "Stored!"; break; case 2 : jQuery.data(adiv, "blah", 86); value = "Stored!"; break; case 3 : jQuery.removeData(adiv); value = "Removed!"; break; } $("span").text("" + value); }); </pre> <pre> &lt;div&gt;A div&lt;/div&gt; &lt;button&gt;Get "blah" from the div&lt;/button&gt; &lt;button&gt;Set "blah" to "hello"&lt;/button&gt; &lt;button&gt;Set "blah" to 86&lt;/button&gt; &lt;button&gt;Remove "blah" from the div&lt;/button&gt; &lt;p&gt;The "blah" value of this div is &lt;span&gt;?&lt;/span&gt;&lt;/p&gt; </pre> This function can be useful for attaching data to elements without having to create a new expando. It also isn't limited to a string. The value can be any format. To avoid conflicts in plugins, it is usually effective to store one object using the plugin name and put all the necessary information in that object. &lt;code&gt; var obj = jQuery.data($("#target").get(0), "pluginname", {}); obj[...] = ... &lt;/code&gt; DOM element of interest. Name of the data to store. Value to be stored. <p> Store then retrieve a value from the div element. </p> <pre> var adiv = $("div").get(0); jQuery.data(adiv, "test", { first: 16, last: "pizza!" }); $("span:first").text(jQuery.data(adiv, "test").first); $("span:last").text(jQuery.data(adiv, "test").last); </pre> <pre> &lt;div&gt; The values stored were &lt;span&gt;&lt;/span&gt; and &lt;span&gt;&lt;/span&gt; &lt;/div&gt; </pre> This function is not the same as &lt;a href='Core/each'&gt;$().each()&lt;/a&gt; - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything. The callback has two arguments:the key (objects) or index (arrays) as the first, and the value as the second. If you wish to break the each() loop at a particular iteration you can do so by making your function return false. Returning non-false is the same as a &lt;code&gt;continue&lt;/code&gt; statement in a for loop, it will skip immediately to the next iteration. The object or array to iterate over. The function that will be executed on every object. &lt;pre&gt;function callback(indexInArray, valueOfElement) { var booleanKeepGoing; this; // == valueOfElement (casted to Object) return booleanKeepGoing; // optional, unless false // and want to stop looping } &lt;/pre&gt; <p> Iterates through the array displaying each number as both a word and numeral </p> <pre> var arr = [ "one", "two", "three", "four", "five" ]; var obj = { one:1, two:2, three:3, four:4, five:5 }; jQuery.each(arr, function() { $("#" + this).text("My id is " + this + "."); return (this != "four"); // will stop running to skip "five" }); jQuery.each(obj, function(i, val) { $("#" + i).append(document.createTextNode(" - " + val)); }); </pre> <pre> &lt;div id="one"&gt;&lt;/div&gt; &lt;div id="two"&gt;&lt;/div&gt; &lt;div id="three"&gt;&lt;/div&gt; &lt;div id="four"&gt;&lt;/div&gt; &lt;div id="five"&gt;&lt;/div&gt; </pre> <p> Iterates over items in an array, accessing both the current item and its index. </p> <pre> $.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n ); }); </pre> <p> Iterates over the properties in an object, accessing both the current item and its key. </p> <pre> $.each( { name: "John", lang: "JS" }, function(i, n){ alert( "Name: " + i + ", Value: " + n ); }); </pre> Can be used to add functions into the jQuery namespace. See &lt;a href='Core/jQuery.fn.extend'&gt;jQuery.fn.extend&lt;/a&gt; for more information on using this method to add &lt;a href='Plugins/Authoring'&gt;Plugins&lt;/a&gt;. The object that will be merged into the jQuery object. <p> Adds two functions into the jQuery namespace. </p> <pre> jQuery.extend({ min: function(a, b) { return a &lt; b ? a : b; }, max: function(a, b) { return a &gt; b ? a : b; } }); </pre> If no target is specified, the JQuery namespace itself is extended. This can be useful for plugin authors wishing to add new methods to JQuery. If a boolean true is specified as the first argument, JQuery performs a deep copy, recursively copying any objects it finds. Otherwise, the copy will share structure with the original object(s). Undefined properties are not copied. However, properties inherited from the object's prototype ''will'' be copied over. If set, the merge becomes recursive (i.e. deep copy). The object to extend. The object that will be merged into the first. More objects to merge into the first. <p> Merge settings and options, modifying settings. </p> <pre> var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; jQuery.extend(settings, options); </pre> <p> Merge defaults and options, without modifying the defaults. </p> <pre> var empty = {} var defaults = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; var settings = $.extend(empty, defaults, options); </pre> Can be used to add functions into the to add &lt;a href='Plugins/Authoring'&gt;plugin methods (plugins)&lt;/a&gt;. The object that will be merged into the jQuery object. <p> Adds two plugin methods. </p> <pre> jQuery.fn.extend({ check: function() { return this.each(function() { this.checked = true; }); }, uncheck: function() { return this.each(function() { this.checked = false; }); } }); </pre> This is an easy way to send a simple GET request to a server without having to use the more complex $.ajax function. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code). If you need to have both error and success callbacks, you may want to use $.ajax. The URL of the page to load. Key/value pairs that will be sent to the server. A function to be executed whenever the data is loaded successfully. &lt;pre&gt;function (data, textStatus) { // data could be xmlDoc, jsonObj, html, text, etc... this; // the options for this ajax request } &lt;/pre&gt; Type of data to be returned to callback function: "xml", "html", "script", "json", "jsonp", or "text". <p> Request the test.php page, but ignore the return results. </p> <pre> $.get("test.php"); </pre> <p> Request the test.php page and send some additional data along (while still ignoring the return results). </p> <pre> $.get("test.php", { name: "John", time: "2pm" } ); </pre> <p> pass arrays of data to the server (while still ignoring the return results). </p> <pre> $.get("test.php", { 'choices[]': ["Jon", "Susan"]} ); </pre> <p> Alert out the results from requesting test.php (HTML or XML, depending on what was returned). </p> <pre> $.get("test.php", function(data){ alert("Data Loaded: " + data); }); </pre> <p> Alert out the results from requesting test.cgi with an additional payload of data (HTML or XML, depending on what was returned). </p> <pre> $.get("test.cgi", { name: "John", time: "2pm" }, function(data){ alert("Data Loaded: " + data); }); </pre> As of jQuery 1.2, you can load JSON data located on another domain if you specify a [http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/ JSONP] callback, which can be done like so: "myurl?callback=?". jQuery automatically replaces the ? with the correct method name to call, calling your specified callback. Note: Keep in mind, that lines after this function will be executed before callback. The URL of the page to load. Key/value pairs that will be sent to the server. A function to be executed whenever the data is loaded successfully. &lt;pre&gt;function (data, textStatus) { // data will be a jsonObj this; // the options for this ajax request } &lt;/pre&gt; <p> Loads the four most recent cat pictures from the Flickr JSONP API. </p> <pre> $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){ $.each(data.items, function(i,item){ $("&lt;img/&gt;").attr("src", item.media.m).appendTo("#images"); if ( i == 4 ) return false; }); }); </pre> <pre> &lt;div id="images"&gt; &lt;/div&gt; </pre> <p> Load the JSON data from test.js and access a name from the returned JSON data. </p> <pre> $.getJSON("test.js", function(json){ alert("JSON Data: " + json.users[3].name); }); </pre> <p> Load the JSON data from test.js, passing along additional data, and access a name from the returned JSON data. </p> <pre> $.getJSON("test.js", { name: "John", time: "2pm" }, function(json){ alert("JSON Data: " + json.users[3].name); }); </pre> <p> List the result of a consultation of pages.php in HTML as an array. By Manuel Gonzalez. </p> <pre> var id=$("#id").attr("value"); $.getJSON("pages.php",{id:id},dates); function dates(datos) { $("#list").html("Name:"+datos[1].name+"&lt;br&gt;"+"Last Name:"+datos[1].lastname+"&lt;br&gt;"+"Address:"+datos[1].address); } </pre> Before jQuery 1.2, getScript was only able to load scripts from the same domain as the original page. As of 1.2, you can now load JavaScript files from any domain. Warning: Safari 2 and older is unable to evaluate scripts in a global context synchronously. If you load functions via getScript, make sure to call them after a delay. The URL of the page to load. A function to be executed whenever the data is loaded successfully. &lt;pre&gt;function (data, textStatus) { // data should be javascript this; // the options for this ajax request } &lt;/pre&gt; <p> We load the new [http://jquery.com/plugins/project/color official jQuery Color Animation plugin] dynamically and bind some color animations to occur once the new functionality is loaded. </p> <pre> $.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js", function(){ $("#go").click(function(){ $(".block").animate( { backgroundColor: 'pink' }, 1000) .animate( { backgroundColor: 'blue' }, 1000); }); }); </pre> <pre> &lt;button id="go"&gt;&raquo; Run&lt;/button&gt; &lt;div class="block"&gt;&lt;/div&gt; </pre> <p> Load the test.js JavaScript file and execute it. </p> <pre> $.getScript("test.js"); </pre> <p> Load the test.js JavaScript file and execute it, displaying an alert message when the execution is complete. </p> <pre> $.getScript("test.js", function(){ alert("Script loaded and executed."); }); </pre> The filter function will be passed two arguments: The current array item and its index. The filter function must return 'true' to keep the item in the array. The Array to find items in. The function to process each item against. The first argument to the function is the list item, and the second argument is the list index. The function should return a Boolean value. The "lambda-form" function feature was removed in jQuery 1.2.3 to help compatibility with other frameworks. &lt;pre&gt;function callback(elementOfArray, indexInArray) { var shouldKeepIt; this; // == window return shouldKeepIt; } &lt;/pre&gt; If "invert" is false, or not provided, then the function returns an array consisting of all elements for which "callback" returns true. If "invert" is true, then the function returns an array consisting of all elements for which "callback" returns false. <p> Filters the original array of numbers leaving that are not 5 and have an index greater than 3. Then it removes all 9s while inverting it. </p> <pre> var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ]; $("div").text(arr.join(", ")); arr = jQuery.grep(arr, function(n, i){ return (n != 5 && i &gt; 4); }); $("p").text(arr.join(", ")); arr = jQuery.grep(arr, function (a) { return a != 9; }); $("span").text(arr.join(", ")); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;span&gt;&lt;/span&gt; </pre> <p> Filter an array of numbers to include only numbers bigger then zero. </p> <pre> $.grep( [0,1,2], function(n,i){ return n &gt; 0; }); </pre> Value to see if it exists in the array. Array to look through for the value. <p> </p> <pre> var arr = [ 4, "Pete", 8, "John" ]; $("span:eq(0)").text(jQuery.inArray("John", arr)); $("span:eq(1)").text(jQuery.inArray(4, arr)); $("span:eq(2)").text(jQuery.inArray("David", arr)); </pre> <pre> &lt;div&gt;"John" found at &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;4 found at &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;"David" found at &lt;span&gt;&lt;/span&gt;&lt;/div&gt; </pre> Object to test whether or not it is an array. <p> Finds out if the parameter is an array. </p> <pre> $("b").append( "" + $.isArray([]) ); </pre> <pre> Is [] an Array? &lt;b&gt;&lt;/b&gt; </pre> &lt;p&gt;'''Note:''' As of jQuery 1.3 the range of functions supported by isFunction is much smaller (for example, native functions provided by the browser, like 'alert', and DOM methods like 'getAttribute' are no longer guaranteed to be detected as functions).&lt;/p&gt; Object to test whether or not it is a function. <p> Test a few parameter examples. </p> <pre> function stub() { } var objs = [ function () {}, { x:15, y:20 }, null, stub, "function" ]; jQuery.each(objs, function (i) { var isFunc = jQuery.isFunction(objs[i]); $("span").eq(i).text(isFunc); }); </pre> <pre> &lt;div&gt;jQuery.isFunction(objs[0]) = &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;jQuery.isFunction(objs[1]) = &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;jQuery.isFunction(objs[2]) = &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;jQuery.isFunction(objs[3]) = &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;jQuery.isFunction(objs[4]) = &lt;span&gt;&lt;/span&gt;&lt;/div&gt; </pre> <p> Finds out if the parameter is a funcion. </p> <pre> $.isFunction(function(){}); </pre> Typically it will be unnecessary to use this function if you are using jQuery which uses this function internally. Anything to turn in to an actual Array. <p> Turn a collection of HTMLElements into an Array of them. </p> <pre> var arr = jQuery.makeArray(document.getElementsByTagName("div")); arr.reverse(); // use an Array method on list of dom elements $(arr).appendTo(document.body); </pre> <pre> &lt;div&gt;First&lt;/div&gt; &lt;div&gt;Second&lt;/div&gt; &lt;div&gt;Third&lt;/div&gt; &lt;div&gt;Fourth&lt;/div&gt; </pre> The translation function that is provided to this method is called for each item in the array and is passed two arguments: The index within the array, and the item to be translated. The function can then return the translated value, 'null' (to remove the item), or an array of values - which will be flattened into the full array. Map can also iterate through objects as well. The Array to translate. The function to process each item against. The argument to the function is the list item. The function can return any value. The "lambda-form" function represented as a string no longer works. It was removed in version 1.2.3 to increase compatibility with Adobe AIR. &lt;pre&gt;function callback(indexInArray, elementOfArray) { var replacementValue; this; // unmapped return replacementValue; } &lt;/pre&gt; <p> A couple examples of using .map() </p> <pre> var arr = [ "a", "b", "c", "d", "e" ] $("div").text(arr.join(", ")); arr = jQuery.map(arr, function(n, i){ return (n.toUpperCase() + i); }); $("p").text(arr.join(", ")); arr = jQuery.map(arr, function (a) { return a + a; }); $("span").text(arr.join(", ")); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;span&gt;&lt;/span&gt; </pre> <p> Maps the original array to a new one and adds 4 to each value. </p> <pre> $.map( [0,1,2], function(n){ return n + 4; }); </pre> <p> Maps the original array to a new one and adds 1 to each value if it is bigger then zero, otherwise it's removed. </p> <pre> $.map( [0,1,2], function(n){ return n &gt; 0 ? n + 1 : null; }); </pre> <p> Maps the original array to a new one, each element is added with it's original value and the value plus one. </p> <pre> $.map( [0,1,2], function(n){ return [ n, n + 1 ]; }); </pre> <p> Maps the original array to a new one, each element is squared. </p> <pre> $.map( [0,1,2,3], function (a) { return a * a; } ); </pre> The result is the altered first argument with the elements from the second array added. To remove duplicate elements from the resulting array, use $.unique(). The first array to merge, the elements of second added. The second array to merge into the first, unaltered. <p> Merges two arrays, altering the first argument. </p> <pre> $.merge( [0,1,2], [2,3,4] ) </pre> <p> Merges two arrays, altering the first argument. </p> <pre> $.merge( [3,2,1], [4,3,2] ) </pre> This helps to make sure that jQuery doesn't conflict with the $ object of other libraries. By using this function, you will only be able to access jQuery using the 'jQuery' variable. For example, where you used to do $("div p"), you now must do jQuery("div p"). '''NOTE:''' This function must be called after including the jQuery javascript file, but '''before''' including any other conflicting library, and also before actually that other conflicting library gets used, in case jQuery is included last. noConflict can be called at the end of the jQuery.js file to globally disable the $() jQuery alias. jQuery.noConflict returns a reference to jQuery, so it can be used to override the $() alias of the jQuery object. <p> Maps the original object that was referenced by $ back to $. </p> <pre> jQuery.noConflict(); // Do something with jQuery jQuery("div p").hide(); // Do something with another library's $() $("content").style.display = 'none'; </pre> <p> Reverts the $ alias and then creates and executes a function to provide the $ as a jQuery alias inside the functions scope. Inside the function the original $ object is not available. This works well for most plugins that don't rely on any other library. </p> <pre> jQuery.noConflict(); (function($) { $(function() { // more code using $ as alias to jQuery }); })(jQuery); // other code using $ as an alias to the other library </pre> <p> You can chain the jQuery.noConflict() with the shorthand ready for a compact code. </p> <pre> jQuery.noConflict()(function(){ // code using jQuery }); // other code using $ as an alias to the other library </pre> <p> Creates a different alias instead of jQuery to use in the rest of the script. </p> <pre> var j = jQuery.noConflict(); // Do something with jQuery j("div p").hide(); // Do something with another library's $() $("content").style.display = 'none'; </pre> This is a more-extreme version of the simple &lt;a href='Core/jQuery.noConflict'&gt;noConflict&lt;/a&gt; method, as this one will completely undo what jQuery has introduced. This is to be used in an extreme case where you'd like to embed jQuery into a high-conflict environment. '''NOTE:''' It's very likely that plugins won't work after this particular method has been called. Set to true to enable the extreme rollback of jQuery and its variables. <p> Completely move jQuery to a new namespace in another object. </p> <pre> var dom = {}; dom.query = jQuery.noConflict(true); </pre> An Array or jQuery object is serialized by name/value pairs. An object by key/value pairs. <p> Serialize a key/value object. </p> <pre> var params = { width:1680, height:1050 }; var str = jQuery.param(params); $("#results").text(str); </pre> <pre> &lt;div id="results"&gt;&lt;/div&gt; </pre> This is an easy way to send a simple POST request to a server without having to use the more complex $.ajax function. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code). The returned data format can be specified by the fourth parameter. If you need to have both error and success callbacks, you may want to use $.ajax. $.post is a (simplified) wrapper function for $.ajax. The URL of the page to load. Key/value pairs that will be sent to the server. A function to be executed whenever the data is loaded successfully. &lt;pre&gt;function (data, textStatus) { // data could be xmlDoc, jsonObj, html, text, etc... this; // the options for this ajax request // textStatus can be one of: // "timeout" // "error" // "notmodified" // "success" // "parsererror" // NOTE: Apparently, only "success" is returned when you make // an Ajax call in this way. Other errors silently fail. // See above note about using $.ajax. } &lt;/pre&gt; Type of data to be returned to callback function: "xml", "html", "script", "json", "jsonp", or "text". &lt;pre&gt;$.postJSON = function(url, data, callback) { $.post(url, data, callback, "json"); }; &lt;/pre&gt; <p> Request the test.php page, but ignore the return results. </p> <pre> $.post("test.php"); </pre> <p> Request the test.php page and send some additional data along (while still ignoring the return results). </p> <pre> $.post("test.php", { name: "John", time: "2pm" } ); </pre> <p> pass arrays of data to the server (while still ignoring the return results). </p> <pre> $.post("test.php", { 'choices[]': ["Jon", "Susan"] }); </pre> <p> Alert out the results from requesting test.php (HTML or XML, depending on what was returned). </p> <pre> $.post("test.php", function(data){ alert("Data Loaded: " + data); }); </pre> <p> Alert out the results from requesting test.php with an additional payload of data (HTML or XML, depending on what was returned). </p> <pre> $.post("test.php", { name: "John", time: "2pm" }, function(data){ alert("Data Loaded: " + data); }); </pre> <p> Gets the test.php page content, store it in a XMLHttpResponse object and applies the process() JavaScript function. </p> <pre> $.post("test.php", { name: "John", time: "2pm" }, function(data){ process(data); }, "xml"); </pre> <p> Gets the test.php page contents which has been returned in json format (&lt;?php echo json_encode(array("name"=&gt;"John","time"=&gt;"2pm")); ?&gt;) </p> <pre> $.post("test.php", { func: "getNameAndTime" }, function(data){ alert(data.name); // John console.log(data.time); // 2pm }, "json"); </pre> This is the complement function to jQuery.data(elem) which is called as necessary by jQuery.data(elem, name, value). Element to delete the data store from. <p> Set a data store then remove it. </p> <pre> var adiv = $("div").get(0); $("span:eq(0)").text("" + jQuery.data(adiv, "test1")); jQuery.data(adiv, "test1", "VALUE-1"); jQuery.data(adiv, "test2", "VALUE-2"); $("span:eq(1)").text("" + jQuery.data(adiv, "test1")); jQuery.removeData(adiv); $("span:eq(2)").text("" + jQuery.data(adiv, "test1")); $("span:eq(3)").text("" + jQuery.data(adiv, "test2")); </pre> <pre> &lt;div&gt;value1 before creation: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value1 after creation: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value1 after removal: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value2 after removal: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; </pre> This is the complement function to jQuery.data(elem, name, value). Element to delete the named data store property from. The name of the data store property to remove. <p> Set a data store for 2 names then remove one of them. </p> <pre> var adiv = $("div").get(0); $("span:eq(0)").text("" + jQuery.data(adiv, "test1")); jQuery.data(adiv, "test1", "VALUE-1"); jQuery.data(adiv, "test2", "VALUE-2"); $("span:eq(1)").text("" + jQuery.data(adiv, "test1")); jQuery.removeData(adiv, "test1"); $("span:eq(2)").text("" + jQuery.data(adiv, "test1")); $("span:eq(3)").text("" + jQuery.data(adiv, "test2")); </pre> <pre> &lt;div&gt;value1 before creation: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value1 after creation: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value1 after removal: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value2 after removal: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; </pre> Uses a regular expression to remove whitespace from the given string. The string to trim. <p> Removes the two whitespaces at the start and at the end of the string. </p> <pre> $("button").click(function () { var str = " lots of spaces before and after "; alert("'" + str + "'"); str = jQuery.trim(str); alert("'" + str + "' - no longer"); }); </pre> <pre> &lt;button&gt;Show Trim Example&lt;/button&gt; </pre> <p> Removes the two whitespaces at the start and at the end of the string. </p> <pre> $.trim(" hello, how are you? "); </pre> The Array of Elements to translate. <p> Removes any duplicate elements from the array of divs. </p> <pre> var divs = $("div").get(); // add 3 elements of class dup too (they are divs) divs = divs.concat($(".dup").get()); $("div:eq(1)").text("Pre-unique there are " + divs.length + " elements."); divs = jQuery.unique(divs); $("div:eq(2)").text("Post-unique there are " + divs.length + " elements.") .css("color", "red"); </pre> <pre> &lt;div&gt;There are 6 divs in this document.&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div class="dup"&gt;&lt;/div&gt; &lt;div class="dup"&gt;&lt;/div&gt; &lt;div class="dup"&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Removes any duplicate elements from the array of divs. </p> <pre> $.unique(document.getElementsByTagName("div")); </pre> This causes all of the functions that have been bound to the keydown event to be executed, and calls the browser's default keydown action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the keydown event. The keydown event usually fires when a key on the keyboard is pressed. The keydown event fires when a key on the keyboard is pressed. A function to bind to the keydown event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To perform actions in response to keyboard presses on a page, try: </p> <pre> $(window).keydown(function(event){ switch (event.keyCode) { // ... // different keys do different things // Different browsers provide different codes // see here for details: http://unixpapa.com/js/key.html // ... } }); </pre> This causes all of the functions that have been bound to the keypress event to be executed, and calls the browser's default keypress action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the keypress event. The keypress event usually fires when a key on the keyboard is pressed. The keypress event fires when a key on the keyboard is "clicked". A keypress is defined as a keydown and keyup on the same key. The sequence of these events is: &lt;ul&gt;&lt;li&gt;keydown&lt;/li&gt;&lt;li&gt;keypress&lt;/li&gt;&lt;li&gt;keyup&lt;/li&gt;&lt;/ul&gt; A function to bind to the keypress event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Show spaces and letters when typed. </p> <pre> $("input").keypress(function (e) { if (e.which == 32 || (65 &lt;= e.which && e.which &lt;= 65 + 25) || (97 &lt;= e.which && e.which &lt;= 97 + 25)) { var c = String.fromCharCode(e.which); $("p").append($("&lt;span/&gt;")) .children(":last") .append(document.createTextNode(c)); } else if (e.which == 8) { // backspace in IE only be on keydown $("p").children(":last").remove(); } $("div").text(e.which); }); </pre> <pre> &lt;input type="text" /&gt; &lt;p&gt;Add text - &lt;/p&gt; &lt;div&gt;&lt;/div&gt; </pre> This causes all of the functions that have been bound to the keyup event to be executed, and calls the browser's default keyup action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the keyup event. The keyup event usually fires when a key on the keyboard is released. The keyup event fires when a key on the keyboard is released. A function to bind to the keyup event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To perform an action when the escape key has been released: </p> <pre> $(document).keyup(function(event){ if (event.keyCode == 27) { alert('escaped!'); } }); </pre> '''Possible event values:''' &lt;code&gt;click&lt;/code&gt;, &lt;code&gt;dblclick&lt;/code&gt;, &lt;code&gt; mousedown&lt;/code&gt;, &lt;code&gt;mouseup&lt;/code&gt;, &lt;code&gt;mousemove&lt;/code&gt;, &lt;code&gt;mouseover&lt;/code&gt;, &lt;code&gt;mouseout&lt;/code&gt;, &lt;code&gt;keydown&lt;/code&gt;, &lt;code&gt;keypress&lt;/code&gt;, &lt;code&gt;keyup&lt;/code&gt; &lt;br&gt; '''Currently not supported:''' &lt;code&gt;blur&lt;/code&gt;, &lt;code&gt;focus&lt;/code&gt;, &lt;code&gt;mouseenter&lt;/code&gt;, &lt;code&gt;mouseleave&lt;/code&gt;, &lt;code&gt;change&lt;/code&gt;, &lt;code&gt;submit&lt;/code&gt; &lt;p&gt;This method works and behaves very similarly to jQuery's bind method but with one important distinction: When you bind a "live" event it will bind to all current and future elements on the page (using [http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/ event delegation]). For example if you bound a live click to all "li" elements on the page then added another li at a later time - that click event would continue to work for the new element (this is not the case with bind which must be re-bound on all new elements).&lt;/p&gt; &lt;p&gt;.live() behaves similarly to the popular [http://plugins.jquery.com/project/livequery liveQuery] plugin but with a few major differences:&lt;/p&gt; * .live (currently) supports a subset of all events. Note the full list of supported/not-supported events above. * .live doesn't support the no-event style callback that liveQuery provides. Only event handlers can be bound with .live. * .live doesn't have a "setup" or "cleanup" step, since all events are delegated rather than bound directly to an element. &lt;p&gt;To remove a live event you should use the die method.&lt;/p&gt; One or more event types separated by a space A function to bind to the event on each of the set of matched elements &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Click a paragraph to add another. Note that live binds the click event to all paragraphs - even new ones. </p> <pre> $("p").live("click", function(){ $(this).after("&lt;p&gt;Another paragraph!&lt;/p&gt;"); }); </pre> <pre> &lt;p&gt;Click me!&lt;/p&gt; &lt;span&gt;&lt;/span&gt; </pre> <p> To display each paragraph's text in an alert box whenever it is clicked: </p> <pre> $("p").live("click", function(){ alert( $(this).text() ); }); </pre> <p> To cancel a default action and prevent it from bubbling up, return false: </p> <pre> $("a").live("click", function() { return false; }) </pre> <p> To cancel only the default action by using the preventDefault method. </p> <pre> $("a").live("click", function(event){ event.preventDefault(); }); </pre> <p> Can bind custom events too. </p> <pre> $("p").live("myCustomEvent", function(e, myName, myValue){ $(this).text("Hi there!"); $("span").stop().css("opacity", 1) .text("myName = " + myName) .fadeIn(30).fadeOut(1000); }); $("button").click(function () { $("p").trigger("myCustomEvent"); }); </pre> <pre> &lt;p&gt;Has an attached custom event.&lt;/p&gt; &lt;button&gt;Trigger custom event&lt;/button&gt; &lt;span style="display:none;"&gt;&lt;/span&gt; </pre> &lt;p&gt;A GET request will be performed by default - but if you pass in any extra parameters then a POST will occur.&lt;/p&gt; &lt;p&gt;In jQuery 1.2 you can now specify a jQuery selector in the URL. Doing so will filter the incoming HTML document, only injecting the elements that match the selector. The syntax looks something like "url #some &gt; selector". See the examples for more information.&lt;/p&gt; The URL of the HTML page to load. Key/value pairs that will be sent to the server. As of jQuery 1.3 a data string can be passed in instead. The function called when the ajax request is complete (not necessarily success). &lt;pre&gt;function (responseText, textStatus, XMLHttpRequest) { this; // dom element } &lt;/pre&gt; <p> Load a piece of the documentation sidebar navigation into a custom unordered list. </p> <pre> $("#links").load("/Main_Page #jq-p-Getting-Started li"); </pre> <pre> &lt;b&gt;jQuery Links:&lt;/b&gt; &lt;ul id="links"&gt;&lt;/ul&gt; </pre> <p> Load the feeds.html file into the div with the ID of feeds. </p> <pre> $("#feeds").load("feeds.html"); </pre> <p> pass arrays of data to the server. </p> <pre> $("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } ); </pre> <p> Same as above, but will POST the additional parameters to the server and a callback that is executed when the server is finished responding. </p> <pre> $("#feeds").load("feeds.php", {limit: 25}, function(){ alert("The last 25 entries in the feed have been loaded"); }); </pre> When bound to the window element, the event fires when the user agent finishes loading all content within a document, including window, frames, objects and images. For elements, it fires when the target element and all of its content has finished loading. Note: load will work only if you set it before the element has completely loaded, if you set it after that nothing will happen. This doesn't happen in $(document).ready(), which jQuery handles it to work as expected, also when setting it after the DOM has loaded. A function to bind to the load event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Run a function when the page is fully loaded including graphics. </p> <pre> $(window).load(function () { // run code }); </pre> <p> Add the class bigImg to all images with size greater then 100 upon each image load. </p> <pre> $('img.userIcon').load(function(){ if($(this).height() &gt; 100) { $(this).addClass('bigImg'); } }); </pre> You could use this to build lists of values, attributes, css values - or even perform special, custom, selector transformations. This is provided as a convenience method for using &lt;a href='Utilities/jQuery.map'&gt;$.map()&lt;/a&gt;. The function to execute on each element in the set. &lt;pre&gt;function callback(index, domElement) { var replacement; this; // also dom element // replacement == null : delete spot // replacement == array : insert the elements of the array // else replace the spot with replacement return replacement; } &lt;/pre&gt; <p> Build a list of all the values within a form. </p> <pre> $("p").append( $("input").map(function(){ return $(this).val(); }).get().join(", ") ); </pre> <pre> &lt;p&gt;&lt;b&gt;Values: &lt;/b&gt;&lt;/p&gt; &lt;form&gt; &lt;input type="text" name="name" value="John"/&gt; &lt;input type="text" name="password" value="password"/&gt; &lt;input type="text" name="url" value="http://ejohn.org/"/&gt; &lt;/form&gt; </pre> <p> A contrived example to show some functionality. </p> <pre> var mappedItems = $("li").map(function (index) { var replacement = $("&lt;li&gt;").text($(this).text()).get(0); if (index == 0) { // make the first item all caps $(replacement).text($(replacement).text().toUpperCase()); } else if (index == 1 || index == 3) { // delete the second and fourth items replacement = null; } else if (index == 2) { // make two of the third item and add some text replacement = [replacement,$("&lt;li&gt;").get(0)]; $(replacement[0]).append("&lt;b&gt; - A&lt;/b&gt;"); $(replacement[1]).append("Extra &lt;b&gt; - B&lt;/b&gt;"); } // replacment will be an dom element, null, // or an array of dom elements return replacement; }); $("#results").append(mappedItems); </pre> <pre> &lt;ul&gt; &lt;li&gt;First&lt;/li&gt; &lt;li&gt;Second&lt;/li&gt; &lt;li&gt;Third&lt;/li&gt; &lt;li&gt;Fourth&lt;/li&gt; &lt;li&gt;Fifth&lt;/li&gt; &lt;/ul&gt; &lt;ul id="results"&gt; &lt;/ul&gt; </pre> The mousedown event fires when the pointing device button is pressed over an element. A function to bind to the mousedown event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Show texts when mouseup and mousedown event triggering. </p> <pre> $("p").mouseup(function(){ $(this).append('&lt;span style="color:#F00;"&gt;Mouse up.&lt;/span&gt;'); }).mousedown(function(){ $(this).append('&lt;span style="color:#00F;"&gt;Mouse down.&lt;/span&gt;'); }); </pre> <pre> &lt;p&gt;Press mouse and release here.&lt;/p&gt; </pre> The mousemove event fires when the pointing device is moved while it is over an element. The event handler is passed one variable - the event object. Its .clientX and .clientY properties represent the coordinates of the mouse. A function to bind to the mousmove event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Show the mouse coordinates when the mouse is moved over the yellow div. Coordinates are relative to the window which in this case is the iframe. </p> <pre> $("div").mousemove(function(e){ var pageCoords = "( " + e.pageX + ", " + e.pageY + " )"; var clientCoords = "( " + e.clientX + ", " + e.clientY + " )"; $("span:first").text("( e.pageX, e.pageY ) - " + pageCoords); $("span:last").text("( e.clientX, e.clientY ) - " + clientCoords); }); </pre> <pre> &lt;p&gt; Try scrolling too. &lt;span&gt;Move the mouse over the div.&lt;/span&gt; &lt;span&gt;&nbsp;&lt;/span&gt; &lt;/p&gt; &lt;div&gt;&lt;/div&gt; </pre> The mouseout event fires when the pointing device is moved away from an element. A function to bind to the mouseout event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Show texts when mouseover and mouseout event triggering. '''Mouseout''' fires when the pointer moves into or out from child element, while '''mouseleave''' doesn't. </p> <pre> var i = 0; $("div.overout").mouseout(function(){ $("p:first",this).text("mouse out"); $("p:last",this).text(++i); }).mouseover(function(){ $("p:first",this).text("mouse over"); }); var n = 0; $("div.enterleave").bind("mouseenter",function(){ $("p:first",this).text("mouse enter"); }).bind("mouseleave",function(){ $("p:first",this).text("mouse leave"); $("p:last",this).text(++n); }); </pre> <pre> &lt;div class="out overout"&gt;&lt;p&gt;move your mouse&lt;/p&gt;&lt;div class="in overout"&gt;&lt;p&gt;move your mouse&lt;/p&gt;&lt;p&gt;0&lt;/p&gt;&lt;/div&gt;&lt;p&gt;0&lt;/p&gt;&lt;/div&gt; &lt;div class="out enterleave"&gt;&lt;p&gt;move your mouse&lt;/p&gt;&lt;div class="in enterleave"&gt;&lt;p&gt;move your mouse&lt;/p&gt;&lt;p&gt;0&lt;/p&gt;&lt;/div&gt;&lt;p&gt;0&lt;/p&gt;&lt;/div&gt; </pre> The mouseover event fires when the pointing device is moved onto an element. A function to bind to the mouseover event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Show texts when mouseover and mouseout event triggering. '''Mouseover''' fires when the pointer moves into or out from child element, while '''mouseenter''' does't. </p> <pre> var i = 0; $("div.overout").mouseover(function(){ $("p:first",this).text("mouse over"); $("p:last",this).text(++i); }).mouseout(function(){ $("p:first",this).text("mouse out"); }); var n = 0; $("div.enterleave").bind("mouseenter",function(){ $("p:first",this).text("mouse enter"); $("p:last",this).text(++n); }).bind("mouseleave",function(){ $("p:first",this).text("mouse leave"); }); </pre> <pre> &lt;div class="out overout"&gt;&lt;p&gt;move your mouse&lt;/p&gt;&lt;div class="in overout"&gt;&lt;p&gt;move your mouse&lt;/p&gt;&lt;p&gt;0&lt;/p&gt;&lt;/div&gt;&lt;p&gt;0&lt;/p&gt;&lt;/div&gt; &lt;div class="out enterleave"&gt;&lt;p&gt;move your mouse&lt;/p&gt;&lt;div class="in enterleave"&gt;&lt;p&gt;move your mouse&lt;/p&gt;&lt;p&gt;0&lt;/p&gt;&lt;/div&gt;&lt;p&gt;0&lt;/p&gt;&lt;/div&gt; </pre> The mouseup event fires when the pointing device button is released over an element. A function to bind to the mouseup event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Show texts when mouseup and mousedown event triggering. </p> <pre> $("p").mouseup(function(){ $(this).append('&lt;span style="color:#F00;"&gt;Mouse up.&lt;/span&gt;'); }).mousedown(function(){ $(this).append('&lt;span style="color:#00F;"&gt;Mouse down.&lt;/span&gt;'); }); </pre> <pre> &lt;p&gt;Press mouse and release here.&lt;/p&gt; </pre> next only returns the very next sibling for each element, not all next siblings (see nextAll). You may provide an optional expression to filter the returned set. An expression with which to filter the returned set. <p> Find the very next sibling of each disabled button and change its text "this button is disabled". </p> <pre> $("button[disabled]").next().text("this button is disabled"); </pre> <pre> &lt;div&gt;&lt;button disabled="disabled"&gt;First&lt;/button&gt; - &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;&lt;button&gt;Second&lt;/button&gt; - &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;&lt;button disabled="disabled"&gt;Third&lt;/button&gt; - &lt;span&gt;&lt;/span&gt;&lt;/div&gt; </pre> <p> Find the very next sibling of each paragraph that has a class "selected". </p> <pre> $("p").next(".selected").css("background", "yellow"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p class="selected"&gt;Hello Again&lt;/p&gt; &lt;div&gt;&lt;span&gt;And Again&lt;/span&gt;&lt;/div&gt; </pre> Use an optional expression to filter the matched set. An expression to filter the next Elements with. <p> Locate all the divs after the first and give them a class. </p> <pre> $("div:first").nextAll().addClass("after"); </pre> <pre> &lt;div&gt;first&lt;/div&gt; &lt;div&gt;sibling&lt;div&gt;child&lt;/div&gt;&lt;/div&gt; &lt;div&gt;sibling&lt;/div&gt; &lt;div&gt;sibling&lt;/div&gt; </pre> <p> Locate all the paragraphs after the second child in the body and give them a class. </p> <pre> $(":nth-child(1)").nextAll("p").addClass("after"); </pre> <pre> &lt;p&gt;p&lt;/p&gt; &lt;div&gt;div&lt;/div&gt; &lt;p&gt;p&lt;/p&gt; &lt;p&gt;p&lt;/p&gt; &lt;div&gt;div&lt;/div&gt; &lt;p&gt;p&lt;/p&gt; &lt;div&gt;div&lt;/div&gt; </pre> An expression with which to remove matching elements, an element to remove from the set or a set of elements to remove from the jQuery set of matched elements. <p> Adds a border to divs that are not green or blue. </p> <pre> $("div").not(".green, #blueone") .css("border-color", "red"); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div id="blueone"&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div class="green"&gt;&lt;/div&gt; &lt;div class="green"&gt;&lt;/div&gt; &lt;div class="gray"&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Removes the element with the ID "selected" from the set of all paragraphs. </p> <pre> $("p").not( $("#selected")[0] ) </pre> <p> Removes the element with the ID "selected" from the set of all paragraphs. </p> <pre> $("p").not("#selected") </pre> <p> Removes all elements that match "div p.selected" from the total set of all paragraphs. </p> <pre> $("p").not($("div p.selected")) </pre> The returned object contains two &lt;a href='Types#Integer'&gt;Integer&lt;/a&gt; properties, top and left. The method works only with visible elements. <p> Access the offset of the second paragraph: </p> <pre> var p = $("p:last"); var offset = p.offset(); p.html( "left: " + offset.left + ", top: " + offset.top ); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;p&gt;2nd Paragraph&lt;/p&gt; </pre> <p> Click to see the offset. </p> <pre> $("*", document.body).click(function (e) { var offset = $(this).offset(); e.stopPropagation(); $("#result").text(this.tagName + " coords ( " + offset.left + ", " + offset.top + " )"); }); </pre> <pre> &lt;div id="result"&gt;Click an element.&lt;/div&gt; &lt;p&gt; This is the best way to &lt;span&gt;find&lt;/span&gt; an offset. &lt;/p&gt; &lt;div class="abs"&gt; &lt;/dov&gt; </pre> This is the first parent of the element that has position (as in relative or absolute). This method only works with visible elements. &lt;p&gt;The handler is executed only once for each element. Otherwise, the same rules as described in &lt;a href='Events/bind'&gt;bind&lt;/a&gt;() apply. The event handler is passed an event object that you can use to prevent default behaviour. To stop both default action and event bubbling, your handler should return false.&lt;/p&gt;&lt;p&gt;In most cases, you can define your event handlers as anonymous functions (see first example). In cases where that is not possible, you can pass additional data as the second paramter (and the handler function as the third), see second example.&lt;/p&gt; An event type Additional data passed to the event handler as event.data A function to bind to the specified event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Tie a one-time click to each div. </p> <pre> var n = 0; $("div").one("click", function(){ var index = $("div").index(this); $(this).css({ borderStyle:"inset", cursor:"auto" }); $("p").text("Div at index #" + index + " clicked." + " That's " + ++n + " total clicks."); }); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;p&gt;Click a green square...&lt;/p&gt; </pre> <p> To display the text of all paragraphs in an alert box the first time each of them is clicked: </p> <pre> $("p").one("click", function(){ alert( $(this).text() ); }); </pre> This method works for both visible and hidden elements. A set of key/value pairs that configure the outerHeight method. All options are optional. <p> Get outerHeight </p> <pre> var p = $("p:first"); $("p:last").text( "outerHeight:" + p.outerHeight() + " , outerHeight(true):" + p.outerHeight(true) ); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;p&gt;&lt;/p&gt; </pre> This method works for both visible and hidden elements. The margin can be included by passing an options map with margin set to true. A set of key/value pairs that configure the outerWidth method. All options are optional. <p> Get outerWidth </p> <pre> var p = $("p:first"); $("p:last").text( "outerWidth:" + p.outerWidth()+ " , outerWidth(true):" + p.outerWidth(true) ); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;p&gt;&lt;/p&gt; </pre> You may use an optional expression to filter the set of parent elements that will match. If there is no parent, returns a jQuery object with a length of 0. An expression to filter the parents with. <p> Shows the parent of each element as (parent &gt; child). Check the View Source to see the raw html. </p> <pre> $("*", document.body).each(function () { var parentTag = $(this).parent().get(0).tagName; $(this).prepend(document.createTextNode(parentTag + " &gt; ")); }); </pre> <pre> &lt;div&gt;div, &lt;span&gt;span, &lt;/span&gt; &lt;b&gt;b &lt;/b&gt; &lt;/div&gt; &lt;p&gt;p, &lt;span&gt;span, &lt;em&gt;em &lt;/em&gt; &lt;/span&gt; &lt;/p&gt; &lt;div&gt;div, &lt;strong&gt;strong, &lt;span&gt;span, &lt;/span&gt; &lt;em&gt;em, &lt;b&gt;b, &lt;/b&gt; &lt;/em&gt; &lt;/strong&gt; &lt;b&gt;b &lt;/b&gt; &lt;/div&gt; </pre> <p> Find the parent element of each paragraph with a class "selected". </p> <pre> $("p").parent(".selected").css("background", "yellow"); </pre> <pre> &lt;div&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;/div&gt; &lt;div class="selected"&gt;&lt;p&gt;Hello Again&lt;/p&gt;&lt;/div&gt; </pre> An expression to filter the ancestors with <p> Find all parent elements of each b. </p> <pre> var parentEls = $("b").parents() .map(function () { return this.tagName; }) .get().join(", "); $("b").append("&lt;strong&gt;" + parentEls + "&lt;/strong&gt;"); </pre> <pre> &lt;div&gt; &lt;p&gt; &lt;span&gt; &lt;b&gt;My parents are: &lt;/b&gt; &lt;/span&gt; &lt;/p&gt; &lt;/div&gt; </pre> <p> Click to find all unique div parent elements of each span. </p> <pre> function showParents() { $("div").css("border-color", "white"); var len = $("span.selected") .parents("div") .css("border", "2px red solid") .length; $("b").text("Unique div parents: " + len); } $("span").click(function () { $(this).toggleClass("selected"); showParents(); }); </pre> <pre> &lt;p&gt; &lt;div&gt; &lt;div&gt;&lt;span&gt;Hello&lt;/span&gt;&lt;/div&gt; &lt;span&gt;Hello Again&lt;/span&gt; &lt;/div&gt; &lt;div&gt; &lt;span&gt;And Hello Again&lt;/span&gt; &lt;/div&gt; &lt;/p&gt; &lt;b&gt;Click Hellos to toggle their parents.&lt;/b&gt; </pre> The returned object contains two &lt;a href='Types#Integer'&gt;Integer&lt;/a&gt; properties, top and left. For accurate calculations make sure to use pixel values for margins, borders and padding. This method only works with visible elements. <p> Access the position of the second paragraph: </p> <pre> var p = $("p:first"); var position = p.position(); $("p:last").text( "left: " + position.left + ", top: " + position.top ); </pre> <pre> &lt;div&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt; </pre> This operation is the best way to insert elements inside, at the beginning, of all matched elements. Content to prepend to the target. <p> Prepends some HTML to all paragraphs. </p> <pre> $("p").prepend("&lt;b&gt;Hello &lt;/b&gt;"); </pre> <pre> &lt;p&gt;there friend!&lt;/p&gt; &lt;p&gt;amigo!&lt;/p&gt; </pre> <p> Prepends a DOM Element to all paragraphs. </p> <pre> $("p").prepend(document.createTextNode("Hello ")); </pre> <pre> &lt;p&gt;is what I'd say&lt;/p&gt; &lt;p&gt;is what I said&lt;/p&gt; </pre> <p> Prepends a jQuery object (similar to an Array of DOM Elements) to all paragraphs. </p> <pre> $("p").prepend( $("b") ); </pre> <pre> &lt;p&gt; is what was said.&lt;/p&gt;&lt;b&gt;Hello&lt;/b&gt; </pre> This operation is, essentially, the reverse of doing a regular $(A).prepend(B), in that instead of prepending B to A, you're prepending A to B. target to which the content will be prepended. <p> Prepends all spans to the element with the ID "foo" </p> <pre> $("span").prependTo("#foo"); // check prepend() examples </pre> <pre> &lt;div id="foo"&gt;FOO!&lt;/div&gt; &lt;span&gt;I have something to say... &lt;/span&gt; </pre> Use an optional expression to filter the matched set. Only the immediately previous sibling is returned, not all previous siblings. An expression to filter the previous Elements with. <p> Find the very previous sibling of each div. </p> <pre> var $curr = $("#start"); $curr.css("background", "#f99"); $("button").click(function () { $curr = $curr.prev(); $("div").css("background", ""); $curr.css("background", "#f99"); }); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;span&gt;has child&lt;/span&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div id="start"&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;p&gt;&lt;button&gt;Go to Prev&lt;/button&gt;&lt;/p&gt; </pre> <p> Find the very previous sibling of each paragraph that has a class "selected". </p> <pre> $("p").prev(".selected").css("background", "yellow"); </pre> <pre> &lt;div&gt;&lt;span&gt;Hello&lt;/span&gt;&lt;/div&gt; &lt;p class="selected"&gt;Hello Again&lt;/p&gt; &lt;p&gt;And Again&lt;/p&gt; </pre> Use an optional expression to filter the matched set. An expression to filter the previous elements with. <p> Locate all the divs in front of the last div and give them a class. </p> <pre> $("div:last").prevAll().addClass("before"); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> The queue name (fx by default) <p> Show the length of the queue. </p> <pre> $("#show").click(function () { var n = $("div").queue("fx"); $("span").text("Queue length is: " + n.length); }); function runIt() { $("div").show("slow"); $("div").animate({left:'+=200'},2000); $("div").slideToggle(1000); $("div").slideToggle("fast"); $("div").animate({left:'-=200'},1500); $("div").hide("slow"); $("div").show(1200); $("div").slideUp("normal", runIt); } runIt(); </pre> <pre> &lt;button id="show"&gt;Show Length of Queue&lt;/button&gt; &lt;span&gt;&lt;/span&gt; &lt;div&gt;&lt;/div&gt; </pre> The queue name (fx by default) The function to add to the queue. &lt;pre&gt;function callback() { this; // dom element // to continue the queue you must call jQuery(this).dequeue(); } &lt;/pre&gt; <p> Queue a custom function. </p> <pre> $(document.body).click(function () { $("div").show("slow"); $("div").animate({left:'+=200'},2000); $("div").queue(function () { $(this).addClass("newcolor"); $(this).dequeue(); }); $("div").animate({left:'-=200'},500); $("div").queue(function () { $(this).removeClass("newcolor"); $(this).dequeue(); }); $("div").slideUp(); }); </pre> <pre> Click here... &lt;div&gt;&lt;/div&gt; </pre> The queue name (fx by default) The queue to replace all the queues with. The functions have the same parameters and this value as queue(callback). <p> Set a queue array to delete the queue. </p> <pre> $("#start").click(function () { $("div").show("slow"); $("div").animate({left:'+=200'},5000); $("div").queue(function () { $(this).addClass("newcolor"); $(this).dequeue(); }); $("div").animate({left:'-=200'},1500); $("div").queue(function () { $(this).removeClass("newcolor"); $(this).dequeue(); }); $("div").slideUp(); }); $("#stop").click(function () { $("div").queue("fx", []); $("div").stop(); }); </pre> <pre> &lt;button id="start"&gt;Start&lt;/button&gt; &lt;button id="stop"&gt;Stop&lt;/button&gt; &lt;div&gt;&lt;/div&gt; </pre> &lt;p&gt;This is probably the most important function included in the event module, as it can greatly improve the response times of your web applications.&lt;/p&gt;&lt;p&gt;In a nutshell, this is a solid replacement for using window.onload, and attaching a function to that. By using this method, your bound function will be called the instant the DOM is ready to be read and manipulated, which is when 99.99% of all JavaScript code needs to run.&lt;/p&gt;&lt;p&gt;There is one argument passed to the ready event handler: A reference to the jQuery function. You can name that argument whatever you like, and can therefore stick with the $ alias without risk of naming collisions.&lt;/p&gt;&lt;p&gt;You can have as many $(document).ready events on your page as you like. The functions are then executed in the order they were added.&lt;/p&gt; &lt;p&gt;'''Note:''' Please make sure that all of your stylesheets are included before your scripts (especially those that call the ready function). Doing so will make sure that all of your styling is loaded and ready before your jQuery code begins executing.&lt;/p&gt; The function to be executed when the DOM is ready. &lt;pre&gt;function callback(jQueryReference) { this; // document } &lt;/pre&gt; <p> Display a message when the DOM is loaded. </p> <pre> $(document).ready(function () { $("p").text("The DOM is now loaded and can be manipulated."); }); </pre> <pre> &lt;p&gt; &lt;/p&gt; </pre> <p> To run code when the DOM loads, write: </p> <pre> $(document).ready(function(){ // Your code here... }); </pre> <p> To use both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias, write: </p> <pre> jQuery(function($) { // Your code using failsafe $ alias here... }); </pre> <p> Commonly written as: </p> <pre> $(function() { // Your code here... }); </pre> This does NOT remove them from the jQuery object, allowing you to use the matched elements further. Note that this function starting with 1.2.2 will also remove all event handlers and internally cached data. So: &lt;code&gt; $("#foo").remove().appendTo("#bar"); &lt;/code&gt; should be written as &lt;code&gt; $("#foo").appendTo("#bar"); &lt;/code&gt; to avoid losing the event handlers. Can be filtered with an optional expression. A jQuery expression to filter the set of elements to be removed. <p> Removes all paragraphs from the DOM </p> <pre> $("button").click(function () { $("p").remove(); }); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; how are &lt;p&gt;you?&lt;/p&gt; &lt;button&gt;Call remove() on paragraphs </pre> <p> Removes all paragraphs that contain "Hello" from the DOM </p> <pre> $("button").click(function () { $("p").remove(":contains('Hello')"); }); </pre> <pre> &lt;p class="hello"&gt;Hello&lt;/p&gt; how are &lt;p&gt;you?&lt;/p&gt; &lt;button&gt;Call remove(":contains('Hello')") on paragraphs </pre> The name of the property to remove. <p> Clicking the button enables the input next to it. </p> <pre> $("button").click(function () { $(this).next().removeAttr("disabled") .focus() .val("editable now"); }); </pre> <pre> &lt;button&gt;Enable&lt;/button&gt; &lt;input type="text" disabled="disabled" value="can't edit this" /&gt; </pre> One or more CSS classes to remove from the elements, these are separated by spaces. <p> Remove the class 'blue' from the matched elements. </p> <pre> $("p:even").removeClass("blue"); </pre> <pre> &lt;p class="blue under"&gt;Hello&lt;/p&gt; &lt;p class="blue under highlight"&gt;and&lt;/p&gt; &lt;p class="blue under"&gt;then&lt;/p&gt; &lt;p class="blue under"&gt;Goodbye&lt;/p&gt; </pre> <p> Remove the class 'blue' and 'under' from the matched elements. </p> <pre> $("p:odd").removeClass("blue under"); </pre> <pre> &lt;p class="blue under"&gt;Hello&lt;/p&gt; &lt;p class="blue under highlight"&gt;and&lt;/p&gt; &lt;p class="blue under"&gt;then&lt;/p&gt; &lt;p class="blue under"&gt;Goodbye&lt;/p&gt; </pre> <p> Remove all the classes from the matched elements. </p> <pre> $("p:eq(1)").removeClass(); </pre> <pre> &lt;p class="blue under"&gt;Hello&lt;/p&gt; &lt;p class="blue under highlight"&gt;and&lt;/p&gt; &lt;p class="blue under"&gt;then&lt;/p&gt; &lt;p class="blue under"&gt;Goodbye&lt;/p&gt; </pre> This is the complement function to $(...).data(name, value). The name of the data store property to remove. <p> Set a data store for 2 names then remove one of them. </p> <pre> $("span:eq(0)").text("" + $("div").data("test1")); $("div").data("test1", "VALUE-1"); $("div").data("test2", "VALUE-2"); $("span:eq(1)").text("" + $("div").data("test1")); $("div").removeData("test1"); $("span:eq(2)").text("" + $("div").data("test1")); $("span:eq(3)").text("" + $("div").data("test2")); </pre> <pre> &lt;div&gt;value1 before creation: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value1 after creation: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value1 after removal: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; &lt;div&gt;value2 after removal: &lt;span&gt;&lt;/span&gt;&lt;/div&gt; </pre> This function is the complement to replaceWith() which does the same task with the parameters reversed. The elements to find and replace the matched elements with. <p> Replace all the paragraphs with bold words. </p> <pre> $("&lt;b&gt;Paragraph. &lt;/b&gt;").replaceAll("p"); // check replaceWith() examples </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> Content to replace the matched elements with. <p> On click, replace the button with a div containing the same word. </p> <pre> $("button").click(function () { $(this).replaceWith("&lt;div&gt;" + $(this).text() + "&lt;/div&gt;"); }); </pre> <pre> &lt;button&gt;First&lt;/button&gt; &lt;button&gt;Second&lt;/button&gt; &lt;button&gt;Third&lt;/button&gt; </pre> <p> Replace all the paragraphs with bold words. </p> <pre> $("p").replaceWith("&lt;b&gt;Paragraph. &lt;/b&gt;"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> <p> Replace all the paragraphs with empty div elements. </p> <pre> $("p").replaceWith(document.createElement("div")); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> <p> On click, replace each paragraph with a jQuery div object that is already in the DOM. Notice it doesn't clone the object but rather moves it to replace the paragraph. </p> <pre> $("p").click(function () { $(this).replaceWith($("div")); }); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; &lt;div&gt;Replaced!&lt;/div&gt; </pre> The resize event fires when a document view is resized A function to bind to the resize event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To make resizing the web page window a pain in the neck, try: </p> <pre> $(window).resize(function(){ alert("Stop it!"); }); </pre> The scroll event fires when a document view is scrolled. A function to bind to the scroll event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To do something when your page is scrolled: </p> <pre> $("p").clone().appendTo(document.body); $("p").clone().appendTo(document.body); $("p").clone().appendTo(document.body); $(window).scroll(function () { $("span").css("display", "inline").fadeOut("slow"); }); </pre> <pre> &lt;div&gt;Try scrolling the iframe.&lt;/div&gt; &lt;p&gt;Paragraph - &lt;span&gt;Scroll happened!&lt;/span&gt;&lt;/p&gt; </pre> This method works for both visible and hidden elements. <p> Get scrollLeft </p> <pre> var p = $("p:first"); $("p:last").text( "scrollLeft:" + p.scrollLeft() ); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;p&gt;&lt;/p&gt; </pre> This method works for both visible and hidden elements. A positive number representing the desired scroll left offset. <p> Get scrollLeft </p> <pre> $("div.demo").scrollLeft(300); </pre> <pre> &lt;div class="demo"&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;/div&gt; </pre> This method works for both visible and hidden elements. <p> Get scrollTop </p> <pre> var p = $("p:first"); $("p:last").text( "scrollTop:" + p.scrollTop() ); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt;&lt;p&gt;&lt;/p&gt; </pre> This method works for both visible and hidden elements. A positive number representing the desired scroll top offset. <p> Get scrollTop </p> <pre> $("div.demo").scrollTop(300); </pre> <pre> &lt;div class="demo"&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;/div&gt; </pre> This causes all of the functions that have been bound to that select event to be executed, and calls the browser's default select action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the select event. Note: Do not confuse the "select" event with the "&lt;a href='Events/change'&gt;change&lt;/a&gt;" event, which is the one triggered when an html "select" element is having its selected option modified by the user. <p> To trigger the select event on all input elements, try: </p> <pre> $("input").select(); </pre> The select event fires when a user selects some text in a text field, including input and textarea. A function to bind to the select event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To do something when text in input boxes is selected: </p> <pre> $(document).select( function () { $("div").text("Something was selected").show().fadeOut(1000); }); </pre> <pre> &lt;p&gt; Click and drag the mouse to select text in the inputs. &lt;/p&gt; &lt;input type="text" value="Some text" /&gt; &lt;input type="text" value="to test on" /&gt; &lt;div&gt;&lt;/div&gt; </pre> Serialize is typically used to prepare user input data to be posted to a server. The serialized data is in a standard format that is compatible with almost all server side programming languages and frameworks. In order to work properly '''serialize requires that form fields have a name''' attribute. Having only an id will not work. Note the name attribute in this field: &lt;input id="email" name="email" type="text" /&gt; '''Versions''' As of jQuery 1.2 the serialize method correctly serializes forms. For older versions of jQuery, the [http://www.malsup.com/jquery/form/ Form Plugin's] fieldSerialize method should be used. <p> Serialize a form to a query string, that could be sent to a server in an Ajax request. </p> <pre> function showValues() { var str = $("form").serialize(); $("#results").text(str); } $(":checkbox, :radio").click(showValues); $("select").change(showValues); showValues(); </pre> <pre> &lt;form&gt; &lt;select name="single"&gt; &lt;option&gt;Single&lt;/option&gt; &lt;option&gt;Single2&lt;/option&gt; &lt;/select&gt; &lt;br /&gt; &lt;select name="multiple" multiple="multiple"&gt; &lt;option selected="selected"&gt;Multiple&lt;/option&gt; &lt;option&gt;Multiple2&lt;/option&gt; &lt;option selected="selected"&gt;Multiple3&lt;/option&gt; &lt;/select&gt; &lt;br/&gt; &lt;input type="checkbox" name="check" value="check1" id="ch1"/&gt; &lt;label for="ch1"&gt;check1&lt;/label&gt; &lt;input type="checkbox" name="check" value="check2" checked="checked" id="ch2"/&gt; &lt;label for="ch2"&gt;check2&lt;/label&gt; &lt;br /&gt; &lt;input type="radio" name="radio" value="radio1" checked="checked" id="r1"/&gt; &lt;label for="r1"&gt;radio1&lt;/label&gt; &lt;input type="radio" name="radio" value="radio2" id="r2"/&gt; &lt;label for="r2"&gt;radio2&lt;/label&gt; &lt;/form&gt; &lt;p&gt;&lt;tt id="results"&gt;&lt;/tt&gt;&lt;/p&gt; </pre> The returned JSON structure consists of an Array of Objects where each Object contains one or two keys: &lt;tt&gt;name&lt;/tt&gt; for the parameter name and &lt;tt&gt;value&lt;/tt&gt; for the parameter value if set/not empty.&lt;br /&gt;Example: &lt;code&gt; [ {name: 'firstname', value: 'Hello'}, {name: 'lastname', value: 'World'}, {name: 'alias'}, // this one was empty ] &lt;/code&gt; <p> Get the values from a form, iterate through them, and append them to a results display. </p> <pre> function showValues() { var fields = $(":input").serializeArray(); $("#results").empty(); jQuery.each(fields, function(i, field){ $("#results").append(field.value + " "); }); } $(":checkbox, :radio").click(showValues); $("select").change(showValues); showValues(); </pre> <pre> &lt;p&gt;&lt;b&gt;Results:&lt;/b&gt; &lt;span id="results"&gt;&lt;/span&gt;&lt;/p&gt; &lt;form&gt; &lt;select name="single"&gt; &lt;option&gt;Single&lt;/option&gt; &lt;option&gt;Single2&lt;/option&gt; &lt;/select&gt; &lt;select name="multiple" multiple="multiple"&gt; &lt;option selected="selected"&gt;Multiple&lt;/option&gt; &lt;option&gt;Multiple2&lt;/option&gt; &lt;option selected="selected"&gt;Multiple3&lt;/option&gt; &lt;/select&gt;&lt;br/&gt; &lt;input type="checkbox" name="check" value="check1" id="ch1"/&gt; &lt;label for="ch1"&gt;check1&lt;/label&gt; &lt;input type="checkbox" name="check" value="check2" checked="checked" id="ch2"/&gt; &lt;label for="ch2"&gt;check2&lt;/label&gt; &lt;input type="radio" name="radio" value="radio1" checked="checked" id="r1"/&gt; &lt;label for="r1"&gt;radio1&lt;/label&gt; &lt;input type="radio" name="radio" value="radio2" id="r2"/&gt; &lt;label for="r2"&gt;radio2&lt;/label&gt; &lt;/form&gt; </pre> Same as &lt;a href='Effects/show#speedcallback'&gt;show( speed, [callback] )&lt;/a&gt; without animations. Doesn't change anything if the selected elements are all visible. It doesn't matter if the element is hidden via a hide() call, or via a display:none in a stylesheet. <p> Shows all paragraphs. </p> <pre> $("p").show() </pre> <pre> &lt;p style="display:none"&gt;Hello&lt;/p&gt; </pre> &lt;p&gt;The height, width, and opacity of each of the matched elements are changed dynamically according to the specified speed. As of jQuery 1.3 the padding and margin are also animated, creating a smoother effect.&lt;/p&gt; A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). A function to be executed whenever the animation completes; executes once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates all hidden paragraphs to show slowly, completing the animation within 600 milliseconds. </p> <pre> $("button").click(function () { $("p").show("slow"); }); </pre> <pre> &lt;button&gt;Show it&lt;/button&gt; &lt;p style="display: none"&gt;Hello&lt;/p&gt; </pre> <p> Animates all hidden divs to show fastly in order, completing each animation within 200 milliseconds. Once each animation is done, it starts the next one. </p> <pre> $("#showr").click(function () { $("div:eq(0)").show("fast", function () { // use callee so don't have to name the function $(this).next().show("fast", arguments.callee); }); }); $("#hidr").click(function () { $("div").hide(2000); }); </pre> <pre> &lt;button id="showr"&gt;Show&lt;/button&gt; &lt;button id="hidr"&gt;Hide&lt;/button&gt; &lt;div&gt;Hello,&lt;/div&gt; &lt;div&gt;how&lt;/div&gt; &lt;div&gt;are&lt;/div&gt; &lt;div&gt;you?&lt;/div&gt; </pre> <p> Animates all span and input elements to show normally. Once the animation is done, it changes the text. </p> <pre> function doIt() { $("span,div").show("normal"); } $("button").click(doIt); // can pass in function name $("form").submit(function () { if ($("input").val() == "yes") { $("p").show(4000, function () { $(this).text("Ok, DONE! (now showing)"); }); } $("span,div").hide("normal"); return false; // to stop the submit }); </pre> <pre> &lt;button&gt;Do it!&lt;/button&gt; &lt;span&gt;Are you sure? (type 'yes' if you are) &lt;/span&gt; &lt;div&gt; &lt;form&gt; &lt;input type="text" /&gt; &lt;/form&gt; &lt;/div&gt; &lt;p style="display:none;"&gt;I'm hidden...&lt;/p&gt; </pre> An expression to filter the sibling Elements with <p> Find the unique siblings of all yellow li elements in the 3 lists (including other yellow li elements if appropriate). </p> <pre> var len = $(".hilite").siblings() .css("color", "red") .length; $("b").text(len); </pre> <pre> &lt;ul&gt; &lt;li&gt;One&lt;/li&gt; &lt;li&gt;Two&lt;/li&gt; &lt;li class="hilite"&gt;Three&lt;/li&gt; &lt;li&gt;Four&lt;/li&gt; &lt;/ul&gt; &lt;ul&gt; &lt;li&gt;Five&lt;/li&gt; &lt;li&gt;Six&lt;/li&gt; &lt;li&gt;Seven&lt;/li&gt; &lt;/ul&gt; &lt;ul&gt; &lt;li&gt;Eight&lt;/li&gt; &lt;li class="hilite"&gt;Nine&lt;/li&gt; &lt;li&gt;Ten&lt;/li&gt; &lt;li class="hilite"&gt;Eleven&lt;/li&gt; &lt;/ul&gt; &lt;p&gt;Unique siblings: &lt;b&gt;&lt;/b&gt;&lt;/p&gt; </pre> <p> Find all siblings with a class "selected" of each div. </p> <pre> $("p").siblings(".selected").css("background", "yellow"); </pre> <pre> &lt;div&gt;&lt;span&gt;Hello&lt;/span&gt;&lt;/div&gt; &lt;p class="selected"&gt;Hello Again&lt;/p&gt; &lt;p&gt;And Again&lt;/p&gt; </pre> This returns the same number as the '&lt;a href='Core/length'&gt;length&lt;/a&gt;' property of the jQuery object. However, it is slightly slower, so length should be used instead. <p> Count the divs. Click to add more. </p> <pre> $(document.body).click(function () { $(document.body).append($("&lt;div&gt;")); var n = $("div").size(); $("span").text("There are " + n + " divs." + "Click to add more."); }).click(); // trigger the click to start </pre> <pre> &lt;span&gt;&lt;/span&gt; &lt;div&gt;&lt;/div&gt; </pre> Behaves exactly like the built-in Array slice method. Where to start the subset. The first element is at zero. Can be negative to start from the end of the selection. Where to end the subset (does not include the end element itself). If unspecified, ends at the end of the selection. <p> Turns divs yellow based on a random slice. </p> <pre> function colorEm() { var $div = $("div"); var start = Math.floor(Math.random() * $div.length); var end = Math.floor(Math.random() * ($div.length - start)) + start + 1; if (end == $div.length) end = undefined; $div.css("background", ""); if (end) $div.slice(start, end).css("background", "yellow"); else $div.slice(start).css("background", "yellow"); $("span").text('$("div").slice(' + start + (end ? ', ' + end : '') + ').css("background", "yellow");'); } $("button").click(colorEm); </pre> <pre> &lt;button&gt;Turn slice yellow&lt;/button&gt; &lt;span&gt;Click the button!&lt;/span&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Selects all paragraphs, then slices the selection to include only the first element. </p> <pre> $("p").slice(0, 1).wrapInner("&lt;b&gt;&lt;/b&gt;"); </pre> <p> Selects all paragraphs, then slices the selection to include only the first and second element. </p> <pre> $("p").slice(0, 2).wrapInner("&lt;b&gt;&lt;/b&gt;"); </pre> <p> Selects all paragraphs, then slices the selection to include only the second element. </p> <pre> $("p").slice(1, 2).wrapInner("&lt;b&gt;&lt;/b&gt;"); </pre> <p> Selects all paragraphs, then slices the selection to include only the second and third element. </p> <pre> $("p").slice(1).wrapInner("&lt;b&gt;&lt;/b&gt;"); </pre> <p> Selects all paragraphs, then slices the selection to include only the third element. </p> <pre> $("p").slice(-1).wrapInner("&lt;b&gt;&lt;/b&gt;"); </pre> Only the height is adjusted for this animation, causing all matched elements to be revealed in a "sliding" manner. As of jQuery 1.3 the vertical padding and vertical margin are also animated, creating a smoother effect. A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). A function to be executed whenever the animation completes, executes once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates all divs to slide down and show themselves over 600 milliseconds. </p> <pre> $(document.body).click(function () { if ($("div:first").is(":hidden")) { $("div").slideDown("slow"); } else { $("div").hide(); } }); </pre> <pre> Click me! &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Animates all inputs to slide down, completing the animation within 1000 milliseconds. Once the animation is done, the input look is changed especially if it is the middle input which gets the focus. </p> <pre> $("div").click(function () { $(this).css({ borderStyle:"inset", cursor:"wait" }); $("input").slideDown(1000,function(){ $(this).css("border", "2px red inset") .filter(".middle") .css("background", "yellow") .focus(); $("div").css("visibility", "hidden"); }); }); </pre> <pre> &lt;div&gt;Push!&lt;/div&gt; &lt;input type="text" /&gt; &lt;input type="text" class="middle" /&gt; &lt;input type="text" /&gt; </pre> Only the height is adjusted for this animation, causing all matched elements to be hidden or shown in a "sliding" manner. As of jQuery 1.3 the vertical padding and vertical margin are also animated, creating a smoother effect. A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). A function to be executed whenever the animation completes, executes once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates all paragraphs to slide up or down, completing the animation within 600 milliseconds. </p> <pre> $("button").click(function () { $("p").slideToggle("slow"); }); </pre> <pre> &lt;button&gt;Toggle&lt;/button&gt; &lt;p&gt; This is the paragraph to end all paragraphs. You should feel &lt;em&gt;lucky&lt;/em&gt; to have seen such a paragraph in your life. Congratulations! &lt;/p&gt; </pre> <p> Animates divs between dividers with a toggle that makes some appear and some disappear. </p> <pre> $("#aa").click(function () { $("div:not(.still)").slideToggle("slow", function () { var n = parseInt($("span").text(), 10); $("span").text(n + 1); }); }); </pre> <pre> &lt;button id =aa&gt;Toggle&lt;/button&gt; There have been &lt;span&gt;0&lt;/span&gt; toggled divs. &lt;div&gt;&lt;/div&gt;&lt;div class="still"&gt;&lt;/div&gt; &lt;div style="display:none;"&gt;&lt;/div&gt;&lt;div class="still"&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt;&lt;div class="still"&gt;&lt;/div&gt; &lt;div class="hider"&gt;&lt;/div&gt;&lt;div class="still"&gt;&lt;/div&gt; &lt;div class="hider"&gt;&lt;/div&gt;&lt;div class="still"&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> Only the height is adjusted for this animation, causing all matched elements to be hidden in a "sliding" manner. As of jQuery 1.3 the vertical padding and vertical margin are also animated, creating a smoother effect. A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). A function to be executed whenever the animation completes, executes once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates all divs to slide up, completing the animation within 400 milliseconds. </p> <pre> $(document.body).click(function () { if ($("div:first").is(":hidden")) { $("div").show("slow"); } else { $("div").slideUp(); } }); </pre> <pre> Click me! &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; </pre> <p> Animates all paragraph to slide up, completing the animation within 200 milliseconds. Once the animation is done, it displays an alert. </p> <pre> $("button").click(function () { $(this).parent().slideUp("slow", function () { $("#msg").text($("button", this).text() + " has completed."); }); }); </pre> <pre> &lt;div&gt; &lt;button&gt;Hide One&lt;/button&gt; &lt;input type="text" value="One" /&gt; &lt;/div&gt; &lt;div&gt; &lt;button&gt;Hide Two&lt;/button&gt; &lt;input type="text" value="Two" /&gt; &lt;/div&gt; &lt;div&gt; &lt;button&gt;Hide Three&lt;/button&gt; &lt;input type="text" value="Three" /&gt; &lt;/div&gt; &lt;div id="msg"&gt;&lt;/div&gt; </pre> If any animations are queued to run (and the clearQueue argument is not set to true), then they will begin immediately. A Boolean (true/false) that when set to true clears the animation queue, effectively stopping all queued animations. A Boolean (true/false) that when set to true causes the currently playing animation to immediately complete, including resetting original styles on show and hide and calling the callback function <p> Click the Go button once to start the animation, then click the STOP button to stop it where it's currently positioned. Another option is to click several buttons to queue them up and see that stop just kills the currently playing one. </p> <pre> // Start animation $("#go").click(function(){ $(".block").animate({left: '+=100px'}, 2000); }); // Stop animation when button is clicked $("#stop").click(function(){ $(".block").stop(); }); // Start animation in the opposite direction $("#back").click(function(){ $(".block").animate({left: '-=100px'}, 2000); }); </pre> <pre> &lt;button id="go"&gt;Go&lt;/button&gt; &lt;button id="stop"&gt;STOP!&lt;/button&gt; &lt;button id="back"&gt;Back&lt;/button&gt; &lt;div class="block"&gt;&lt;/div&gt; </pre> This causes all of the functions that have been bound to that submit event to be executed, and calls the browser's default submit action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the submit event. <p> To trigger the submit event on the first form on the page, try: </p> <pre> $("form:first").submit(); </pre> The select event fires when a form is submitted A function to bind to the submit event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> If you'd like to prevent forms from being submitted unless a flag variable is set, try: </p> <pre> $("form").submit(function() { if ($("input:first").val() == "correct") { $("span").text("Validated...").show(); return true; } $("span").text("Not valid!").show().fadeOut(1000); return false; }); </pre> <pre> &lt;p&gt;Type 'correct' to validate.&lt;/p&gt; &lt;form action="javascript:alert('success!');"&gt; &lt;div&gt; &lt;input type="text" /&gt; &lt;input type="submit" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;span&gt;&lt;/span&gt; </pre> <p> If you'd like to prevent forms from being submitted unless a flag variable is set, try: </p> <pre> $("form").submit( function () { return this.some_flag_variable; } ); </pre> The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents. Cannot be used on input elements. For input field text use the &lt;a href='Attributes/val#val'&gt;val attribute&lt;/a&gt;. <p> Find the text in the first paragraph (stripping out the html), then set the html of the last paragraph to show it is just text (the red bold is gone). </p> <pre> var str = $("p:first").text(); $("p:last").html(str); </pre> <pre> &lt;p&gt;&lt;b&gt;Test&lt;/b&gt; Paragraph.&lt;/p&gt; &lt;p&gt;&lt;/p&gt; </pre> The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents. Cannot be used on input elements. For input field text use the &lt;a href='Attributes/val#val'&gt;val attribute&lt;/a&gt;. <p> Find the text in the first paragraph (stripping out the html), then set the html of the last paragraph to show it is just text (the red bold is gone). </p> <pre> var str = $("p:first").text(); $("p:last").html(str); </pre> <pre> &lt;p&gt;&lt;b&gt;Test&lt;/b&gt; Paragraph.&lt;/p&gt; &lt;p&gt;&lt;/p&gt; </pre> Similar to html(), but escapes HTML (replace "&lt;" and "&gt;" with their HTML entities). Cannot be used on input elements. For input field text use the &lt;a href='Attributes/val#val'&gt;val attribute&lt;/a&gt;. The text value to set the contents of the element to. <p> Add text to the paragraph (notice the bold tag is escaped). </p> <pre> $("p").text("&lt;b&gt;Some&lt;/b&gt; new text."); </pre> <pre> &lt;p&gt;Test Paragraph.&lt;/p&gt; </pre> Similar to html(), but escapes HTML (replace "&lt;" and "&gt;" with their HTML entities). Cannot be used on input elements. For input field text use the &lt;a href='Attributes/val#val'&gt;val attribute&lt;/a&gt;. The text value to set the contents of the element to. <p> Add text to the paragraph (notice the bold tag is escaped). </p> <pre> $("p").text("&lt;b&gt;Some&lt;/b&gt; new text."); </pre> <pre> &lt;p&gt;Test Paragraph.&lt;/p&gt; </pre> If they are shown, toggle makes them hidden (using the hide method). If they are hidden, toggle makes them shown (using the show method). <p> Toggles all paragraphs. </p> <pre> $("button").click(function () { $("p").toggle(); }); </pre> <pre> &lt;button&gt;Toggle&lt;/button&gt; &lt;p&gt;Hello&lt;/p&gt; &lt;p style="display: none"&gt;Good Bye&lt;/p&gt; </pre> If the switch is true, toggle makes them hidden (using the hide method). If the switch is false, toggle makes them shown (using the show method). A switch to toggle the display on. <p> Shows all paragraphs, then hides them all, back and forth. </p> <pre> var flip = 0; $("button").click(function () { $("p").toggle( flip++ % 2 == 0 ); }); </pre> <pre> &lt;button&gt;Toggle&lt;/button&gt; &lt;p&gt;Hello&lt;/p&gt; &lt;p style="display: none"&gt;Good Bye&lt;/p&gt; </pre> The height, width, and opacity of each of the matched elements are changed dynamically according to the specified speed. As of jQuery 1.3 the padding and margin are also animated, creating a smoother effect. A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). A function to be executed whenever the animation completes, executes once for each element animated against. &lt;pre&gt;function callback() { this; // dom element } &lt;/pre&gt; <p> Animates all paragraphs to be shown if they are hidden and hidden if they are visible, completing the animation within 600 milliseconds. </p> <pre> $("button").click(function () { $("p").toggle("slow"); }); </pre> <pre> &lt;button&gt;Toggle 'em&lt;/button&gt; &lt;p&gt;Hiya&lt;/p&gt; &lt;p&gt;Such interesting text, eh?&lt;/p&gt; </pre> &lt;p&gt;Whenever a matched element is clicked, the first specified function is fired, when clicked again, the second is fired, and so on. All subsequent clicks continue to rotate through the functions.&lt;/p&gt;&lt;p&gt;Use unbind("click") to remove the toggle event.&lt;/p&gt; The function to execute. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; The function to execute. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; The function to execute. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> Click to toggle highlight on the list item. </p> <pre> $("li").toggle( function () { $(this).css({"list-style-type":"disc", "color":"blue"}); }, function () { $(this).css({"list-style-type":"disc", "color":"red"}); }, function () { $(this).css({"list-style-type":"", "color":""}); } ); </pre> <pre> &lt;ul&gt; &lt;li&gt;Go to the store&lt;/li&gt; &lt;li&gt;Pick up dinner&lt;/li&gt; &lt;li&gt;Debug crash&lt;/li&gt; &lt;li&gt;Take a jog&lt;/li&gt; &lt;/ul&gt; </pre> <p> To toggle a style on table cells: </p> <pre> $("td").toggle( function () { $(this).addClass("selected"); }, function () { $(this).removeClass("selected"); } ); </pre> A CSS class to toggle on the elements. <p> Toggle the class 'highlight' when a paragraph is clicked. </p> <pre> $("p").click(function () { $(this).toggleClass("highlight"); }); </pre> <pre> &lt;p class="blue"&gt;Click to toggle&lt;/p&gt; &lt;p class="blue highlight"&gt;highlight&lt;/p&gt; &lt;p class="blue"&gt;on these&lt;/p&gt; &lt;p class="blue"&gt;paragraphs&lt;/p&gt; </pre> A CSS class to toggle on the elements. A boolean value to toggle the class by. <p> Toggle the class 'highlight' on every third click. </p> <pre> var count = 0; $("p").click(function(){ $(this).toggleClass("highlight", count++ % 3 == 0); }); </pre> <pre> &lt;p class="blue"&gt;Click to toggle&lt;/p&gt; &lt;p class="blue highlight"&gt;highlight&lt;/p&gt; &lt;p class="blue"&gt;on these&lt;/p&gt; &lt;p class="blue"&gt;paragraphs&lt;/p&gt; </pre> &lt;p&gt;This will also cause the default action of the browser with the same name (if one exists) to be executed. For example, passing 'submit' to the trigger() function will also cause the browser to submit the form. This default action can be prevented by returning false from one of the functions bound to the event.&lt;/p&gt; &lt;p&gt;Triggered events aren't limited to browser-based events, you can also trigger custom events registered with bind.&lt;/p&gt; &lt;p&gt;The event handlers will receive a fixed (normalized) event object but it won't contain any of the browser-specific attributes (like keyCode, pageX, or pageY).&lt;/p&gt; &lt;p&gt;jQuery also supports &lt;a href='Namespaced_Events'&gt;namespaced events&lt;/a&gt;. These allow you to trigger or unbind specific groups of bound handlers without having to reference them directly. You can add an '''!''' to the end of the event type in order to trigger only handlers that don't have a namespace specified.&lt;/p&gt; '''New in jQuery 1.3:''' &lt;p&gt;All triggered events now bubble up the DOM tree. For example if you trigger an event on a paragraph then it will trigger on that element first, then on the parent element, and its parent, and so on up to the document. The event object will have a .target property equal to the original triggered element. You can prevent the bubbling by calling &lt;a href='Events/jQuery.Event#event.stopPropagation()'&gt;stopPropagation()&lt;/a&gt; or by returning false from your callback.&lt;/p&gt; &lt;p&gt;The event object constructor is now exposed and you can use it to create your own event object. The full list of properties that are available on the event object (passed to the triggered bound handlers) can be found in the &lt;a href='Events/jQuery.Event'&gt;jQuery.Event&lt;/a&gt; documentation.&lt;/p&gt; &lt;P&gt;You have 3 ways of specifying the event type:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;You can pass the event name (type) which is a string &lt;/li&gt; &lt;li&gt;You can also use a &lt;a href='Events/jQuery.Event'&gt;jQuery.Event object&lt;/a&gt;. You can put data into this object and it will reach the triggered handlers. &lt;/li&gt; &lt;li&gt;Finally, you can pass a literal object with data. It will be copied to a real &lt;a href='Events/jQuery.Event'&gt;jQuery.Event object&lt;/a&gt;. Note that you '''must''' specify a ''type'' attribute in this case. &lt;/li&gt; &lt;/ul&gt; An event object or type to trigger. Additional data to pass as arguments (after the event object) to the event handler. <p> Clicks to button #2 also trigger a click for button #1. </p> <pre> $("button:first").click(function () { update($("span:first")); }); $("button:last").click(function () { $("button:first").trigger('click'); update($("span:last")); }); function update(j) { var n = parseInt(j.text(), 10); j.text(n + 1); } </pre> <pre> &lt;button&gt;Button #1&lt;/button&gt; &lt;button&gt;Button #2&lt;/button&gt; &lt;div&gt;&lt;span&gt;0&lt;/span&gt; button #1 clicks.&lt;/div&gt; &lt;div&gt;&lt;span&gt;0&lt;/span&gt; button #2 clicks.&lt;/div&gt; </pre> <p> To submit the first form without using the submit() function, try: </p> <pre> $("form:first").trigger("submit") </pre> <p> To submit the first form without using the submit() function, try: </p> <pre> var event = jQuery.Event("submit"); $("form:first").trigger(event); if ( event.isDefaultPrevented() ) { // Perform an action... } </pre> <p> To pass arbitrary data to an event: </p> <pre> $("p").click( function (event, a, b) { // when a normal click fires, a and b are undefined // for a trigger like below a refers too "foo" and b refers to "bar" } ).trigger("click", ["foo", "bar"]); </pre> <p> To pass arbitrary data through an event object: </p> <pre> var event = jQuery.Event("logged"); event.user = "foo"; event.pass = "bar"; $("body").trigger(event); </pre> <p> Alternate way to pass data through an event object: </p> <pre> $("body").trigger({ type:"logged", user:"foo", pass:"bar" }); </pre> &lt;p&gt;This method behaves very similarly to the trigger method, with two major exceptions:&lt;/p&gt; &lt;p&gt;First, no default browser actions are triggered.&lt;/p&gt;&lt;p&gt;Second, the event is only triggered on the first element within the jQuery collection. This method returns the return value of the triggered handler instead of a chainable jQuery object. Also, if the jQuery collection is empty, this method returns 'undefined'.&lt;/p&gt; An event type to trigger. Additional data to pass as arguments (after the event object) to the event handler. <p> If you called .triggerHandler() on a focus event - the browsers default focus action would not be triggered, only the event handlers bound to the focus event. </p> <pre> $("#old").click(function(){ $("input").trigger("focus"); }); $("#new").click(function(){ $("input").triggerHandler("focus"); }); $("input").focus(function(){ $("&lt;span&gt;Focused!&lt;/span&gt;").appendTo("body").fadeOut(1000); }); </pre> <pre> &lt;button id="old"&gt;.trigger("focus")&lt;/button&gt; &lt;button id="new"&gt;.triggerHandler("focus")&lt;/button&gt;&lt;br/&gt;&lt;br/&gt; &lt;input type="text" value="To Be Focused"/&gt; </pre> &lt;p&gt;Without any arguments, all bound events are removed. If the type is provided, all bound events of that type are removed. If the function that was passed to bind is provided as the second argument, only that specific event handler is removed.&lt;/p&gt;&lt;p&gt;You can also unbind custom events registered with bind.&lt;/p&gt; &lt;p&gt;jQuery also supports &lt;a href='Namespaced_Events'&gt;namespaced events&lt;/a&gt;. These allow you to trigger or unbind specific groups of bound handlers without having to reference them directly.&lt;/p&gt; An event type to unbind. A function to unbind from the event on each of the set of matched elements. <p> Can bind and unbind events to the colored button. </p> <pre> function aClick() { $("div").show().fadeOut("slow"); } $("#bind").click(function () { // could use .bind('click', aClick) instead but for variety... $("#theone").click(aClick) .text("Can Click!"); }); $("#unbind").click(function () { $("#theone").unbind('click', aClick) .text("Does nothing..."); }); </pre> <pre> &lt;button id="theone"&gt;Does nothing...&lt;/button&gt; &lt;button id="bind"&gt;Bind Click&lt;/button&gt; &lt;button id="unbind"&gt;Unbind Click&lt;/button&gt; &lt;div style="display:none;"&gt;Click!&lt;/div&gt; </pre> <p> To unbind all events from all paragraphs, write: </p> <pre> $("p").unbind() </pre> <p> To unbind all click events from all paragraphs, write: </p> <pre> $("p").unbind( "click" ) </pre> <p> To unbind just one previously bound handler, pass the function in as the second argument: </p> <pre> var foo = function () { // code to handle some kind of event }; $("p").bind("click", foo); // ... now foo will be called when paragraphs are clicked ... $("p").unbind("click", foo); // ... foo will no longer be called. </pre> function to bind to the unload event on each of the matched elements. &lt;pre&gt;function callback(eventObject) { this; // dom element } &lt;/pre&gt; <p> To display an alert when a page is unloaded: </p> <pre> $(window).unload( function () { alert("Bye now!"); } ); </pre> In jQuery 1.2, a value is now returned for all elements, including selects. For multiple selects an array of values is returned. For selects and checkboxes, see the &lt;a href='Selectors/selected'&gt;:selected&lt;/a&gt; and &lt;a href='Selectors/checked'&gt;:checked&lt;/a&gt; selectors, for example: &lt;pre&gt; $('select#foo option:selected').val(); // get the value from a dropdown select $('input:checkbox:checked').val(); // get the value from a checked checkbox $('input:radio[name=bar]:checked').val(); // get the value from a set of radio buttons &lt;/pre&gt; &lt;small&gt;For older versions of jQuery use the [http://www.malsup.com/jquery/form/#fields fieldValue function of the Form Plugin].&lt;/small&gt; <p> Get the single value from a single select and an array of values from a multiple select and display their values. </p> <pre> function displayVals() { var singleValues = $("#single").val(); var multipleValues = $("#multiple").val() || []; $("p").html("&lt;b&gt;Single:&lt;/b&gt; " + singleValues + " &lt;b&gt;Multiple:&lt;/b&gt; " + multipleValues.join(", ")); } $("select").change(displayVals); displayVals(); </pre> <pre> &lt;p&gt;&lt;/p&gt; &lt;select id="single"&gt; &lt;option&gt;Single&lt;/option&gt; &lt;option&gt;Single2&lt;/option&gt; &lt;/select&gt; &lt;select id="multiple" multiple="multiple"&gt; &lt;option selected="selected"&gt;Multiple&lt;/option&gt; &lt;option&gt;Multiple2&lt;/option&gt; &lt;option selected="selected"&gt;Multiple3&lt;/option&gt; &lt;/select&gt; </pre> <p> Find the value of an input box. </p> <pre> $("input").keyup(function () { var value = $(this).val(); $("p").text(value); }).keyup(); </pre> <pre> &lt;input type="text" value="some text"/&gt; &lt;p&gt;&lt;/p&gt; </pre> In jQuery 1.2, this is also able to set the value of select elements, but selecting the appropriate options. The value to set on the matched element. <p> Set the value of an input box. </p> <pre> $("button").click(function () { var text = $(this).text(); $("input").val(text); }); </pre> <pre> &lt;div&gt; &lt;button&gt;Feed&lt;/button&gt; &lt;button&gt;the&lt;/button&gt; &lt;button&gt;Input&lt;/button&gt; &lt;/div&gt; &lt;input type="text" value="click a button" /&gt; </pre> The set of values to check/select. <p> Set a single select and a multiple select . </p> <pre> $("#single").val("Single2"); $("#multiple").val(["Multiple2", "Multiple3"]); $("input").val(["check1","check2", "radio1" ]); </pre> <pre> &lt;select id="single"&gt; &lt;option&gt;Single&lt;/option&gt; &lt;option&gt;Single2&lt;/option&gt; &lt;/select&gt; &lt;select id="multiple" multiple="multiple"&gt; &lt;option selected="selected"&gt;Multiple&lt;/option&gt; &lt;option&gt;Multiple2&lt;/option&gt; &lt;option selected="selected"&gt;Multiple3&lt;/option&gt; &lt;/select&gt;&lt;br/&gt; &lt;input type="checkbox" name="checkboxname" value="check1"/&gt; check1 &lt;input type="checkbox" name="checkboxname" value="check2"/&gt; check2 &lt;input type="radio" name="r" value="radio1"/&gt; radio1 &lt;input type="radio" name="r" value="radio2"/&gt; radio2 </pre> In jQuery 1.2, this method is able to find the width of the window and document. <p> Show various widths. Note the values are from the iframe so might be smaller than you expected. The yellow highlight shows the iframe body. </p> <pre> function showWidth(ele, w) { $("div").text("The width for the " + ele + " is " + w + "px."); } $("#getp").click(function () { showWidth("paragraph", $("p").width()); }); $("#getd").click(function () { showWidth("document", $(document).width()); }); $("#getw").click(function () { showWidth("window", $(window).width()); }); </pre> <pre> &lt;button id="getp"&gt;Get Paragraph Width&lt;/button&gt; &lt;button id="getd"&gt;Get Document Width&lt;/button&gt; &lt;button id="getw"&gt;Get Window Width&lt;/button&gt; &lt;div&gt;&nbsp;&lt;/div&gt; &lt;p&gt; Sample paragraph to test width &lt;/p&gt; </pre> If no explicit unit was specified (like 'em' or '%') then "px" is concatenated to the value. Set the CSS 'width' property to the specified value. <p> To set the width of each div on click to 30px plus a color change. </p> <pre> $("div").one('click', function () { $(this).width(30) .css({cursor:"auto", "background-color":"blue"}); }); </pre> <pre> &lt;div&gt;&lt;/div&gt; &lt;div&gt;d&lt;/div&gt; &lt;div&gt;d&lt;/div&gt; &lt;div&gt;d&lt;/div&gt; &lt;div&gt;d&lt;/div&gt; </pre> This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document. This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest descendant element within its structure -- it is that element that will enwrap everything else. A string of HTML that will be created on the fly and wrapped around each target. <p> Wrap a new div around all of the paragraphs. </p> <pre> $("p").wrap("&lt;div&gt;&lt;/div&gt;"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> <p> Wraps a newly created tree of objects around the spans. Notice anything in between the spans gets left out like the &lt;strong&gt; (red text) in this example. Even the white space between spans is left out. Click View Source to see the original html. </p> <pre> $("span").wrap("&lt;div&gt;&lt;div&gt;&lt;p&gt;&lt;em&gt;&lt;b&gt;&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;"); </pre> <pre> &lt;span&gt;Span Text&lt;/span&gt; &lt;strong&gt;What about me?&lt;/strong&gt; &lt;span&gt;Another One&lt;/span&gt; </pre> A DOM element that will be wrapped around each target. <p> Wrap a new div around all of the paragraphs. </p> <pre> $("p").wrap(document.createElement("div")); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> <p> Wrap a jQuery object double depth div around all of the paragraphs. Notice it doesn't move the object but just clones it to wrap around its target. </p> <pre> $("p").wrap($(".doublediv")); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; &lt;div class="doublediv"&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt; </pre> This is different from &lt;a href='Manipulation/wrap'&gt;.wrap()&lt;/a&gt; where each element in the matched set would get wrapped with an element. This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document. This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest descendant element within its structure -- it is that element that will enwrap everything else. A string of HTML that will be created on the fly and wrapped around the target. <p> Wrap a new div around all of the paragraphs. </p> <pre> $("p").wrapAll("&lt;div&gt;&lt;/div&gt;"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> <p> Wraps a newly created tree of objects around the spans. Notice anything in between the spans gets left out like the &lt;strong&gt; (red text) in this example. Even the white space between spans is left out. Click View Source to see the original html. </p> <pre> $("span").wrapAll("&lt;div&gt;&lt;div&gt;&lt;p&gt;&lt;em&gt;&lt;b&gt;&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;"); </pre> <pre> &lt;span&gt;Span Text&lt;/span&gt; &lt;strong&gt;What about me?&lt;/strong&gt; &lt;span&gt;Another One&lt;/span&gt; </pre> This is different from &lt;a href='Manipulation/wrap'&gt;.wrap()&lt;/a&gt; where each element in the matched set would get wrapped with an element. A DOM element that will be wrapped around the target. <p> Wrap a new div around all of the paragraphs. </p> <pre> $("p").wrapAll(document.createElement("div")); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> <p> Wrap a jQuery object double depth div around all of the paragraphs. Notice it doesn't move the object but just clones it to wrap around its target. </p> <pre> $("p").wrapAll($(".doublediv")); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; &lt;div class="doublediv"&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt; </pre> This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document. This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest ancestor element within its structure -- it is that element that will enwrap everything else. A string of HTML that will be created on the fly and wrapped around the target. <p> Selects all paragraphs and wraps a bold tag around each of its contents. </p> <pre> $("p").wrapInner("&lt;b&gt;&lt;/b&gt;"); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> <p> Wraps a newly created tree of objects around the inside of the body. </p> <pre> $("body").wrapInner("&lt;div&gt;&lt;div&gt;&lt;p&gt;&lt;em&gt;&lt;b&gt;&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;"); </pre> <pre> Plain old text, or is it? </pre> A DOM element that will be wrapped around the target. <p> Selects all paragraphs and wraps a bold tag around each of its contents. </p> <pre> $("p").wrapInner(document.createElement("b")); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre> <p> Selects all paragraphs and wraps a jQuery object around each of its contents. </p> <pre> $("p").wrapInner($("&lt;span class='red'&gt;&lt;/span&gt;")); </pre> <pre> &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;cruel&lt;/p&gt; &lt;p&gt;World&lt;/p&gt; </pre>