This function accepts a string containing a CSS selector, basic XPath, or raw HTML, which is then used to match a set of elements. The HTML string is different from the traditional selectors in that it creates the DOM elements representing that HTML string, on the fly, to be (assumedly) inserted into the document later.
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 or XPath), which then finds all matching elements and remembers them for later use.
By default, $() looks for DOM elements within the context of the current HTML document.
jQuery
This finds all p elements that are children of a div element.
$("div > p")<p>one</p> <div><p>two</p></div> <p>three</p>
[ <p>two</p> ]
Creates a div element (and all of its contents) dynamically, and appends it to the element with the ID of body. Internally, an element is created and it's innerHTML property set to the given markup. It is therefore both quite flexible and limited.
$("<div><p>Hello</p></div>").appendTo("#body")Wrap jQuery functionality around a specific DOM Element. This function also accepts XML Documents and Window objects as valid arguments (even though they are not DOM Elements).
jQuery
$(document).find("div > p")<p>one</p> <div><p>two</p></div> <p>three</p>
[ <p>two</p> ]
Sets the background color of the page to black.
$(document.body).background( "black" );
Wrap jQuery functionality around a set of DOM Elements.
jQuery
Hides all the input elements within a form
$( myForm.elements ).hide()
A shorthand for $(document).ready(), allowing 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 all of the other $() operations on your page. 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.
jQuery
Executes the function when the DOM is ready to be used.
$(function(){
// Document is ready
});A means of creating a cloned copy of a jQuery object. This function copies the set of matched elements from one jQuery object and creates another, new, jQuery object containing the same elements.
jQuery
Locates all p elements with all div elements, without disrupting the original jQuery object contained in 'div' (as would normally be the case if a simple div.find("p") was done).
var div = $("div");
$( div ).find("p");This function accepts a string containing a CSS selector, or basic XPath, which is then used to match a set of elements with the context of the specified DOM element, or document
jQuery
This finds all div elements within the specified XML document.
$("div", xml.responseXML)Execute a function within the context of every matched element. 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 element.
Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set.
jQuery
$("img").each(function(){
this.src = "test.jpg";
});<img/> <img/>
<img src="test.jpg"/> <img src="test.jpg"/>
$("img").each(function(i){
alert( "Image #" + i + " is " + this );
});<img/> <img/>
<img src="test.jpg"/> <img src="test.jpg"/>
Reduce the set of matched elements to a single element. The position of the element in the set of matched elements starts at 0 and goes to length - 1.
jQuery
$("p").eq(1)<p>This is just a test.</p><p>So is this</p>
[ <p>So is this</p> ]
Access all matched elements. 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).
Array<Element>
$("img").get();<img src="test1.jpg"/> <img src="test2.jpg"/>
[ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
Access a single matched element. num is used to access the Nth element matched.
Element
$("img").get(1);<img src="test1.jpg"/> <img src="test2.jpg"/>
[ <img src="test1.jpg"/> ]
Reduce the set of matched elements to all elements after a given position. The position of the element in the set of matched elements starts at 0 and goes to length - 1.
jQuery
$("p").gt(0)<p>This is just a test.</p><p>So is this</p>
[ <p>So is this</p> ]
Searches every matched element for the object and returns the index of the element, if found, starting with zero. Returns -1 if the object wasn't found.
Number
$("*").index(document.getElementById('foobar'))<div id="foobar"></div><b></b><span id="foo"></span>
0
$("*").index(document.getElementById('foo'))<div id="foobar"></div><b></b><span id="foo"></span>
2
$("*").index(document.getElementById('bar'))<div id="foobar"></div><b></b><span id="foo"></span>
-1
The number of elements currently matched.
Number
$("img").length;<img src="test1.jpg"/> <img src="test2.jpg"/>
2
Reduce the set of matched elements to all elements before a given position. The position of the element in the set of matched elements starts at 0 and goes to length - 1.
jQuery
$("p").lt(1)<p>This is just a test.</p><p>So is this</p>
[ <p>This is just a test.</p> ]
The number of elements currently matched.
Number
$("img").size();<img src="test1.jpg"/> <img src="test2.jpg"/>
2
Get the current href of the first matched element.
String
$("a").href();<a href="test.html">my link</a>
"test.html"
Set the href of every matched element.
jQuery
$("a").href("test2.html");<a href="test.html">my link</a>
<a href="test2.html">my link</a>
Get the html contents of the first matched element.
String
$("div").html();<div><input/></div>
<input/>
Set the html contents of every matched element.
jQuery
$("div").html("<b>new stuff</b>");<div><input/></div>
<div><b>new stuff</b></div>
Get the current id of the first matched element.
String
$("input").id();<input type="text" id="test" value="some text"/>
"test"
Set the id of every matched element.
jQuery
$("input").id("newid");<input type="text" id="test" value="some text"/>
<input type="text" id="newid" value="some text"/>
Get the current name of the first matched element.
String
$("input").name();<input type="text" name="username"/>
"username"
Set the name of every matched element.
jQuery
$("input").name("user");<input type="text" name="username"/>
<input type="text" name="user"/>
Get the current rel of the first matched element.
String
$("a").rel();<a href="test.html" rel="nofollow">my link</a>
"nofollow"
Set the rel of every matched element.
jQuery
$("a").rel("nofollow");<a href="test.html">my link</a>
<a href="test.html" rel="nofollow">my link</a>
Get the current src of the first matched element.
String
$("img").src();<img src="test.jpg" title="my image"/>
"test.jpg"
Set the src of every matched element.
jQuery
$("img").src("test2.jpg");<img src="test.jpg" title="my image"/>
<img src="test2.jpg" title="my image"/>
Get the current title of the first matched element.
String
$("img").title();<img src="test.jpg" title="my image"/>
"my image"
Set the title of every matched element.
jQuery
$("img").title("new title");<img src="test.jpg" title="my image"/>
<img src="test.jpg" title="new image"/>
Get the current value of the first matched element.
String
$("input").val();<input type="text" value="some text"/>
"some text"
Set the value of every matched element.
jQuery
$("input").val("test");<input type="text" value="some text"/>
<input type="text" value="test"/>
Insert any number of dynamically generated elements after each of the matched elements.
jQuery
$("p").after("<b>Hello</b>");<p>I would like to say: </p>
<p>I would like to say: </p><b>Hello</b>
Insert an element after each of the matched elements.
jQuery
$("p").after( $("#foo")[0] );<b id="foo">Hello</b><p>I would like to say: </p>
<p>I would like to say: </p><b id="foo">Hello</b>
Insert any number of elements after each of the matched elements.
jQuery
$("p").after( $("b") );<b>Hello</b><p>I would like to say: </p>
<p>I would like to say: </p><b>Hello</b>
Append any number of elements to the inside of every matched elements, generated from the provided HTML. This operation is similar to doing an appendChild to all the specified elements, adding them into the document.
jQuery
$("p").append("<b>Hello</b>");<p>I would like to say: </p>
<p>I would like to say: <b>Hello</b></p>
Append an element to the inside of all matched elements. This operation is similar to doing an appendChild to all the specified elements, adding them into the document.
jQuery
$("p").append( $("#foo")[0] );<p>I would like to say: </p><b id="foo">Hello</b>
<p>I would like to say: <b id="foo">Hello</b></p>
Append any number of elements to the inside of all matched elements. This operation is similar to doing an appendChild to all the specified elements, adding them into the document.
jQuery
$("p").append( $("b") );<p>I would like to say: </p><b>Hello</b>
<p>I would like to say: <b>Hello</b></p>
Append all of the matched elements to another, specified, set of elements. 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.
jQuery
$("p").appendTo("#foo");<p>I would like to say: </p><div id="foo"></div>
<div id="foo"><p>I would like to say: </p></div>
Insert any number of dynamically generated elements before each of the matched elements.
jQuery
$("p").before("<b>Hello</b>");<p>I would like to say: </p>
<b>Hello</b><p>I would like to say: </p>
Insert an element before each of the matched elements.
jQuery
$("p").before( $("#foo")[0] );<p>I would like to say: </p><b id="foo">Hello</b>
<b id="foo">Hello</b><p>I would like to say: </p>
Insert any number of elements before each of the matched elements.
jQuery
$("p").before( $("b") );<p>I would like to say: </p><b>Hello</b>
<b>Hello</b><p>I would like to say: </p>
Create cloned copies of all matched DOM Elements. This does not create a cloned copy of this particular jQuery object, instead it creates duplicate copies of all DOM Elements. This is useful for moving copies of the elements to another location in the DOM.
jQuery
$("b").clone().prependTo("p");<b>Hello</b><p>, how are you?</p>
<b>Hello</b><p><b>Hello</b>, how are you?</p>
Removes all child nodes from the set of matched elements.
jQuery
$("p").empty()<p>Hello, <span>Person</span> <a href="#">and person</a></p>
[ <p></p> ]
Insert all of the matched elements after another, specified, set of elements. 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.
jQuery
$("p").insertAfter("#foo");<p>I would like to say: </p><div id="foo">Hello</div>
<div id="foo">Hello</div><p>I would like to say: </p>
Insert all of the matched elements before another, specified, set of elements. 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.
jQuery
$("p").insertBefore("#foo");<div id="foo">Hello</div><p>I would like to say: </p>
<p>I would like to say: </p><div id="foo">Hello</div>
Prepend any number of elements to the inside of every matched elements, generated from the provided HTML. This operation is the best way to insert dynamically created elements inside, at the beginning, of all the matched element.
jQuery
$("p").prepend("<b>Hello</b>");<p>I would like to say: </p>
<p><b>Hello</b>I would like to say: </p>
Prepend an element to the inside of all matched elements. This operation is the best way to insert an element inside, at the beginning, of all the matched element.
jQuery
$("p").prepend( $("#foo")[0] );<p>I would like to say: </p><b id="foo">Hello</b>
<p><b id="foo">Hello</b>I would like to say: </p>
Prepend any number of elements to the inside of all matched elements. This operation is the best way to insert a set of elements inside, at the beginning, of all the matched element.
jQuery
$("p").prepend( $("b") );<p>I would like to say: </p><b>Hello</b>
<p><b>Hello</b>I would like to say: </p>
Prepend all of the matched elements to another, specified, set of elements. 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.
jQuery
$("p").prependTo("#foo");<p>I would like to say: </p><div id="foo"><b>Hello</b></div>
<div id="foo"><p>I would like to say: </p><b>Hello</b></div>
Removes all matched elements from the DOM. This does NOT remove them from the jQuery object, allowing you to use the matched elements further.
jQuery
$("p").remove();<p>Hello</p> how are <p>you?</p>
how are
Removes only elements (out of the list of matched elements) that match the specified jQuery expression. This does NOT remove them from the jQuery object, allowing you to use the matched elements further.
jQuery
$("p").remove(".hello");<p class="hello">Hello</p> how are <p>you?</p>
how are <p>you?</p>
Wrap all matched elements with a structure of other elements. This wrapping process is most useful for injecting additional stucture 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 en-wrap everything else.
This does not work with elements that contain text. Any necessary text must be added after the wrapping is done.
jQuery
$("p").wrap("<div class='wrap'></div>");<p>Test Paragraph.</p>
<div class='wrap'><p>Test Paragraph.</p></div>
Wrap all matched elements with a structure of other elements. This wrapping process is most useful for injecting additional stucture into a document, without ruining the original semantic qualities of a document.
This works by going through the first element provided and finding the deepest ancestor element within its structure - it is that element that will en-wrap everything else.
This does not work with elements that contain text. Any necessary text must be added after the wrapping is done.
jQuery
$("p").wrap( document.getElementById('content') );<p>Test Paragraph.</p><div id="content"></div>
<div id="content"><p>Test Paragraph.</p></div>
Adds the elements matched by the expression to the jQuery object. This can be used to concatenate the result sets of two expressions.
jQuery
$("p").add("span")<p>Hello</p><p><span>Hello Again</span></p>
[ <p>Hello</p>, <span>Hello Again</span> ]
Adds each of the Elements in the array to the set of matched elements. This is used to add a set of Elements to a jQuery object.
jQuery
$("p").add([document.getElementById("a"), document.getElementById("b")])<p>Hello</p><p><span id="a">Hello Again</span><span id="b">And Again</span></p>
[ <p>Hello</p>, <span id="a">Hello Again</span>, <span id="b">And Again</span> ]
Adds a single Element to the set of matched elements. This is used to add a single Element to a jQuery object.
jQuery
$("p").add( document.getElementById("a") )<p>Hello</p><p><span id="a">Hello Again</span></p>
[ <p>Hello</p>, <span id="a">Hello Again</span> ]
Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element).
jQuery
$("span").ancestors()<html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
[ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
Get a set of elements containing the unique ancestors of the matched set of elements, and filtered by an expression.
jQuery
$("span").ancestors("p")<html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
[ <p><span>Hello</span></p> ]
Get a set of elements containing all of the unique children of each of the matched set of elements.
jQuery
$("div").children()<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
[ <span>Hello Again</span> ]
Get a set of elements containing all of the unique children of each of the matched set of elements, and filtered by an expression.
jQuery
$("div").children(".selected")<div><span>Hello</span><p class="selected">Hello Again</p><p>And Again</p></div>
[ <p class="selected">Hello Again</p> ]
Filter the set of elements to those that contain the specified text.
jQuery
$("p").contains("test")<p>This is just a test.</p><p>So is this</p>
[ <p>This is just a test.</p> ]
End the most recent 'destructive' operation, reverting the list of matched elements back to its previous state. After an end operation, the list of matched elements will revert to the last state of matched elements.
jQuery
$("p").find("span").end();<p><span>Hello</span>, how are you?</p>
$("p").find("span").end() == [ <p>...</p> ]Removes all elements from the set of matched elements that do not match the specified expression. This method is used to narrow down the results of a search.
All searching is done using a jQuery expression. The expression can be written using CSS 1-3 Selector syntax, or basic XPath.
jQuery
$("p").filter(".selected")<p class="selected">Hello</p><p>How are you?</p>
$("p").filter(".selected") == [ <p class="selected">Hello</p> ]Removes all elements from the set of matched elements that do not match at least one of the expressions passed to the function. This method is used when you want to filter the set of matched elements through more than one expression.
Elements will be retained in the jQuery object if they match at least one of the expressions passed.
jQuery
$("p").filter([".selected", ":first"])<p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
$("p").filter([".selected", ":first"]) == [ <p>Hello</p>, <p class="selected">And Again</p> ]Searches for all elements that match the specified expression. This method is the optimal way of finding additional descendant elements with which to process.
All searching is done using a jQuery expression. The expression can be written using CSS 1-3 Selector syntax, or basic XPath.
jQuery
$("p").find("span");<p><span>Hello</span>, how are you?</p>
$("p").find("span") == [ <span>Hello</span> ]Checks the current selection against an expression and returns true, if the selection fits the given expression. Does return false, if the selection does not fit or the expression is not valid.
Boolean
Returns true, because the parent of the input is a form element
$("input[@type='checkbox']").parent().is("form")<form><input type="checkbox" /></form>
true
Returns false, because the parent of the input is a p element
$("input[@type='checkbox']").parent().is("form")<form><p><input type="checkbox" /></p></form>
false
An invalid expression always returns false.
$("form").is(null)<form></form>
false
Get a set of elements containing the unique next siblings of each of the matched set of elements.
It only returns the very next sibling, not all next siblings.
jQuery
$("p").next()<p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
[ <p>Hello Again</p>, <div><span>And Again</span></div> ]
Get a set of elements containing the unique next siblings of each of the matched set of elements, and filtered by an expression.
It only returns the very next sibling, not all next siblings.
jQuery
$("p").next(".selected")<p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>
[ <p class="selected">Hello Again</p> ]
Removes the specified Element from the set of matched elements. This method is used to remove a single Element from a jQuery object.
jQuery
$("p").not( document.getElementById("selected") )<p>Hello</p><p id="selected">Hello Again</p>
[ <p>Hello</p> ]
Removes elements matching the specified expression from the set of matched elements. This method is used to remove one or more elements from a jQuery object.
jQuery
$("p").not("#selected")<p>Hello</p><p id="selected">Hello Again</p>
[ <p>Hello</p> ]
Get a set of elements containing the unique parents of the matched set of elements.
jQuery
$("p").parent()<div><p>Hello</p><p>Hello</p></div>
[ <div><p>Hello</p><p>Hello</p></div> ]
Get a set of elements containing the unique parents of the matched set of elements, and filtered by an expression.
jQuery
$("p").parent(".selected")<div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>
[ <div class="selected"><p>Hello Again</p></div> ]
Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element).
jQuery
$("span").ancestors()<html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
[ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]
Get a set of elements containing the unique ancestors of the matched set of elements, and filtered by an expression.
jQuery
$("span").ancestors("p")<html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
[ <p><span>Hello</span></p> ]
Get a set of elements containing the unique previous siblings of each of the matched set of elements.
It only returns the immediately previous sibling, not all previous siblings.
jQuery
$("p").prev()<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
[ <div><span>Hello Again</span></div> ]
Get a set of elements containing the unique previous siblings of each of the matched set of elements, and filtered by an expression.
It only returns the immediately previous sibling, not all previous siblings.
jQuery
$("p").prev(".selected")<div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
[ <div><span>Hello</span></div> ]
Get a set of elements containing all of the unique siblings of each of the matched set of elements.
jQuery
$("div").siblings()<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
[ <p>Hello</p>, <p>And Again</p> ]
Get a set of elements containing all of the unique siblings of each of the matched set of elements, and filtered by an expression.
jQuery
$("div").siblings(".selected")<div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
[ <p class="selected">Hello Again</p> ]
Adds the specified class to each of the set of matched elements.
jQuery
$("p").addClass("selected")<p>Hello</p>
[ <p class="selected">Hello</p> ]
Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element.
Object
$("img").attr("src");<img src="test.jpg"/>
test.jpg
Set a hash of key/value object properties to all matched elements. This serves as the best way to set a large number of properties on all matched elements.
jQuery
$("img").attr({ src: "test.jpg", alt: "Test Image" });<img/>
<img src="test.jpg" alt="Test Image"/>
Set a single property to a value, on all matched elements.
jQuery
$("img").attr("src","test.jpg");<img/>
<img src="test.jpg"/>
Remove an attribute from each of the matched elements.
jQuery
$("input").removeAttr("disabled")<input disabled="disabled"/>
<input/>
Removes the specified class from the set of matched elements.
jQuery
$("p").removeClass("selected")<p class="selected">Hello</p>
[ <p>Hello</p> ]
Retrieve the text contents of all matched elements. The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents.
String
$("p").text();<p>Test Paragraph.</p>
Test Paragraph.
Adds the specified class if it is present, removes it if it is not present.
jQuery
$("p").toggleClass("selected")<p>Hello</p><p class="selected">Hello Again</p>
[ <p class="selected">Hello</p>, <p>Hello Again</p> ]
Get the current CSS background of the first matched element.
String
$("p").background();<p style="background:blue;">This is just a test.</p>
"blue"
Set the CSS background of every matched element.
jQuery
$("p").background("blue");<p>This is just a test.</p>
<p style="background:blue;">This is just a test.</p>
Get the current CSS color of the first matched element.
String
$("p").color();<p>This is just a test.</p>
"black"
Set the CSS color of every matched element.
jQuery
$("p").color("blue");<p>This is just a test.</p>
<p style="color:blue;">This is just a test.</p>
Access a style property on the first matched element. This method makes it easy to retrieve a style property value from the first matched element.
Object
Retrieves the color style of the first paragraph
$("p").css("color");<p style="color:red;">Test Paragraph.</p>
red
Retrieves the font-weight style of the first paragraph. Note that for all style properties with a dash (like 'font-weight'), you have to write it in camelCase. In other words: Every time you have a '-' in a property, remove it and replace the next character with an uppercase representation of itself. Eg. fontWeight, fontSize, fontFamily, borderWidth, borderStyle, borderBottomWidth etc.
$("p").css("fontWeight");<p style="font-weight: bold;">Test Paragraph.</p>
bold
Set a hash of key/value style properties to all matched elements. This serves as the best way to set a large number of style properties on all matched elements.
jQuery
$("p").css({ color: "red", background: "blue" });<p>Test Paragraph.</p>
<p style="color:red; background:blue;">Test Paragraph.</p>
Set a single style property to a value, on all matched elements.
jQuery
Changes the color of all paragraphs to red
$("p").css("color","red");<p>Test Paragraph.</p>
<p style="color:red;">Test Paragraph.</p>
Get the current CSS float of the first matched element.
String
$("p").float();<p>This is just a test.</p>
"none"
Set the CSS float of every matched element.
jQuery
$("p").float("left");<p>This is just a test.</p>
<p style="float:left;">This is just a test.</p>
Get the current CSS height of the first matched element.
String
$("p").height();<p>This is just a test.</p>
"14px"
Set the CSS height of every matched element. Be sure to include the "px" (or other unit of measurement) after the number that you specify, otherwise you might get strange results.
jQuery
$("p").height("20px");<p>This is just a test.</p>
<p style="height:20px;">This is just a test.</p>
Get the current CSS left of the first matched element.
String
$("p").left();<p>This is just a test.</p>
"0px"
Set the CSS left of every matched element. Be sure to include the "px" (or other unit of measurement) after the number that you specify, otherwise you might get strange results.
jQuery
$("p").left("20px");<p>This is just a test.</p>
<p style="left:20px;">This is just a test.</p>
Get the current CSS overflow of the first matched element.
String
$("p").overflow();<p>This is just a test.</p>
"none"
Set the CSS overflow of every matched element.
jQuery
$("p").overflow("auto");<p>This is just a test.</p>
<p style="overflow:auto;">This is just a test.</p>
Get the current CSS position of the first matched element.
String
$("p").position();<p>This is just a test.</p>
"static"
Set the CSS position of every matched element.
jQuery
$("p").position("relative");<p>This is just a test.</p>
<p style="position:relative;">This is just a test.</p>
Get the current CSS top of the first matched element.
String
$("p").top();<p>This is just a test.</p>
"0px"
Set the CSS top of every matched element. Be sure to include the "px" (or other unit of measurement) after the number that you specify, otherwise you might get strange results.
jQuery
$("p").top("20px");<p>This is just a test.</p>
<p style="top:20px;">This is just a test.</p>
Get the current CSS width of the first matched element.
String
$("p").width();<p>This is just a test.</p>
"300px"
Set the CSS width of every matched element. Be sure to include the "px" (or other unit of measurement) after the number that you specify, otherwise you might get strange results.
jQuery
$("p").width("20px");<p>This is just a test.</p>
<p style="width:20px;">This is just a test.</p>
Contains flags for the useragent, read from navigator.userAgent. 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.
See <a href="http://davecardwell.co.uk/geekery/javascript/jquery/jqbrowser/"> jQBrowser plugin</a> for advanced browser detection:
Boolean
returns true if the current useragent is some version of microsoft's internet explorer
$.browser.msie
Alerts "this is safari!" only for safari browsers
if($.browser.safari) { $( function() { alert("this is safari!"); } ); }A generic iterator function, which can be used to seemlessly iterate over both objects and arrays. This function is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything.
Object
This is an example of iterating over the items in an array, accessing both the current item and its index.
$.each( [0,1,2], function(i){
alert( "Item #" + i + ": " + this );
});This is an example of iterating over the properties in an Object, accessing both the current item and its key.
$.each( { name: "John", lang: "JS" }, function(i){
alert( "Name: " + i + ", Value: " + this );
});Extend one object with another, returning the original, modified, object. This is a great utility for simple inheritance.
Object
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
jQuery.extend(settings, options);settings == { validate: true, limit: 5, name: "bar" }Filter items out of an array, by using a filter function. The specified function will be passed two arguments: The current array item and the index of the item in the array. The function should return 'true' if you wish to keep the item in the array, false if it should be removed.
Array
$.grep( [0,1,2], function(i){
return i > 0;
});[1, 2]
Translate all items in an array to another array of items. The translation function that is provided to this method is called for each item in the array and is passed one argument: 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.
Array
$.map( [0,1,2], function(i){
return i + 4;
});[4, 5, 6]
$.map( [0,1,2], function(i){
return i > 0 ? i + 1 : null;
});[2, 3]
$.map( [0,1,2], function(i){
return [ i, i + 1 ];
});[0, 1, 1, 2, 2, 3]
Merge two arrays together, removing all duplicates. The final order or the new array is: All the results from the first array, followed by the unique results from the second array.
Array
$.merge( [0,1,2], [2,3,4] )
[0,1,2,3,4]
$.merge( [3,2,1], [4,3,2] )
[3,2,1,4]
Remove the whitespace from the beginning and end of a string.
String
$.trim(" hello, how are you? ");"hello, how are you?"
A function for making your own, custom, animations. 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").
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. Oterwise if the string "hide", "show", or "toggle" is provided, a default animation will be constructed for that property.
jQuery
$("p").animate({
height: 'toggle', opacity: 'toggle'
}, "slow");$("p").animate({
left: 50, opacity: 'show'
}, 500);Fade in all matched elements by adjusting their opacity. 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.
jQuery
$("p").fadeIn("slow");Fade in all matched elements by adjusting their opacity and firing a callback function after completion. 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.
jQuery
$("p").fadeIn("slow",function(){
alert("Animation Done.");
});Fade out all matched elements by adjusting their opacity. 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.
jQuery
$("p").fadeOut("slow");Fade out all matched elements by adjusting their opacity and firing a callback function after completion. 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.
jQuery
$("p").fadeOut("slow",function(){
alert("Animation Done.");
});Fade the opacity of all matched elements to a specified opacity. 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.
jQuery
$("p").fadeTo("slow", 0.5);Fade the opacity of all matched elements to a specified opacity and firing a callback function after completion. 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.
jQuery
$("p").fadeTo("slow", 0.5, function(){
alert("Animation Done.");
});Hide all matched elements using a graceful animation. The height, width, and opacity of each of the matched elements are changed dynamically according to the specified speed.
jQuery
$("p").hide("slow");Hide all matched elements using a graceful animation and firing a callback function after completion. The height, width, and opacity of each of the matched elements are changed dynamically according to the specified speed.
jQuery
$("p").hide("slow",function(){
alert("Animation Done.");
});Show all matched elements using a graceful animation. The height, width, and opacity of each of the matched elements are changed dynamically according to the specified speed.
jQuery
$("p").show("slow");Show all matched elements using a graceful animation and firing a callback function after completion. The height, width, and opacity of each of the matched elements are changed dynamically according to the specified speed.
jQuery
$("p").show("slow",function(){
alert("Animation Done.");
});Reveal all matched elements by adjusting their height. Only the height is adjusted for this animation, causing all matched elements to be revealed in a "sliding" manner.
jQuery
$("p").slideDown("slow");Reveal all matched elements by adjusting their height and firing a callback function after completion. Only the height is adjusted for this animation, causing all matched elements to be revealed in a "sliding" manner.
jQuery
$("p").slideDown("slow",function(){
alert("Animation Done.");
});Toggle the visibility of all matched elements by adjusting their height. Only the height is adjusted for this animation, causing all matched elements to be hidden in a "sliding" manner.
jQuery
$("p").slideToggle("slow");Toggle the visibility of all matched elements by adjusting their height and firing a callback function after completion. Only the height is adjusted for this animation, causing all matched elements to be hidden in a "sliding" manner.
jQuery
$("p").slideToggle("slow",function(){
alert("Animation Done.");
});Hide all matched elements by adjusting their height. Only the height is adjusted for this animation, causing all matched elements to be hidden in a "sliding" manner.
jQuery
$("p").slideUp("slow");Hide all matched elements by adjusting their height and firing a callback function after completion. Only the height is adjusted for this animation, causing all matched elements to be hidden in a "sliding" manner.
jQuery
$("p").slideUp("slow",function(){
alert("Animation Done.");
});Hides each of the set of matched elements if they are shown.
jQuery
$("p").hide()<p>Hello</p>
[ <p style="display: none">Hello</p> ]
var pass = true, div = $("div");
div.hide().each(function(){
if ( this.style.display != "none" ) pass = false;
});
ok( pass, "Hide" );Displays each of the set of matched elements if they are hidden.
jQuery
$("p").show()<p style="display: none">Hello</p>
[ <p style="display: block">Hello</p> ]
Toggles each of the set of matched elements. If they are shown, toggle makes them hidden. If they are hidden, toggle makes them shown.
jQuery
$("p").toggle()<p>Hello</p><p style="display: none">Hello Again</p>
[ <p style="display: none">Hello</p>, <p style="display: block">Hello Again</p> ]
Trigger the error event of each matched element. This causes all of the functions that have been bound to thet error event to be executed.
jQuery
$("p").error();<p onerror="alert('Hello');">Hello</p>alert('Hello');Bind a function to the error event of each matched element.
jQuery
$("p").error( function() { alert("Hello"); } );<p>Hello</p>
<p onerror="alert('Hello');">Hello</p>Bind a function to the load event of each matched element.
jQuery
$("p").load( function() { alert("Hello"); } );<p>Hello</p>
<p onload="alert('Hello');">Hello</p>Bind a function to the error event of each matched element, which will only be executed once. Unlike a call to the normal .error() method, calling .oneerror() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").oneerror( function() { alert("Hello"); } );<p onerror="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first errorBind a function to the load event of each matched element, which will only be executed once. Unlike a call to the normal .load() method, calling .oneload() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").oneload( function() { alert("Hello"); } );<p onload="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first loadBind a function to the resize event of each matched element, which will only be executed once. Unlike a call to the normal .resize() method, calling .oneresize() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").oneresize( function() { alert("Hello"); } );<p onresize="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first resizeBind a function to the scroll event of each matched element, which will only be executed once. Unlike a call to the normal .scroll() method, calling .onescroll() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onescroll( function() { alert("Hello"); } );<p onscroll="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first scrollBind a function to the unload event of each matched element, which will only be executed once. Unlike a call to the normal .unload() method, calling .oneunload() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").oneunload( function() { alert("Hello"); } );<p onunload="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first unloadTrigger the resize event of each matched element. This causes all of the functions that have been bound to thet resize event to be executed.
jQuery
$("p").resize();<p onresize="alert('Hello');">Hello</p>alert('Hello');Bind a function to the resize event of each matched element.
jQuery
$("p").resize( function() { alert("Hello"); } );<p>Hello</p>
<p onresize="alert('Hello');">Hello</p>Trigger the scroll event of each matched element. This causes all of the functions that have been bound to thet scroll event to be executed.
jQuery
$("p").scroll();<p onscroll="alert('Hello');">Hello</p>alert('Hello');Bind a function to the scroll event of each matched element.
jQuery
$("p").scroll( function() { alert("Hello"); } );<p>Hello</p>
<p onscroll="alert('Hello');">Hello</p>Removes all bound error events from each of the matched elements.
jQuery
$("p").unerror();<p onerror="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound error event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unerror( myFunction );<p onerror="myFunction">Hello</p>
<p>Hello</p>
Removes all bound load events from each of the matched elements.
jQuery
$("p").unload();<p onload="alert('Hello');">Hello</p><p>Hello</p>
Trigger the unload event of each matched element. This causes all of the functions that have been bound to thet unload event to be executed.
jQuery
$("p").unload();<p onunload="alert('Hello');">Hello</p>alert('Hello');Removes a bound load event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unload( myFunction );<p onload="myFunction">Hello</p>
<p>Hello</p>
Bind a function to the unload event of each matched element.
jQuery
$("p").unload( function() { alert("Hello"); } );<p>Hello</p>
<p onunload="alert('Hello');">Hello</p>Removes all bound resize events from each of the matched elements.
jQuery
$("p").unresize();<p onresize="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound resize event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unresize( myFunction );<p onresize="myFunction">Hello</p>
<p>Hello</p>
Removes all bound scroll events from each of the matched elements.
jQuery
$("p").unscroll();<p onscroll="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound scroll event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unscroll( myFunction );<p onscroll="myFunction">Hello</p>
<p>Hello</p>
Removes all bound unload events from each of the matched elements.
jQuery
$("p").ununload();<p onunload="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound unload event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").ununload( myFunction );<p onunload="myFunction">Hello</p>
<p>Hello</p>
Trigger the change event of each matched element. This causes all of the functions that have been bound to thet change event to be executed.
jQuery
$("p").change();<p onchange="alert('Hello');">Hello</p>alert('Hello');Bind a function to the change event of each matched element.
jQuery
$("p").change( function() { alert("Hello"); } );<p>Hello</p>
<p onchange="alert('Hello');">Hello</p>Bind a function to the change event of each matched element, which will only be executed once. Unlike a call to the normal .change() method, calling .onechange() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onechange( function() { alert("Hello"); } );<p onchange="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first changeBind a function to the select event of each matched element, which will only be executed once. Unlike a call to the normal .select() method, calling .oneselect() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").oneselect( function() { alert("Hello"); } );<p onselect="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first selectBind a function to the submit event of each matched element, which will only be executed once. Unlike a call to the normal .submit() method, calling .onesubmit() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onesubmit( function() { alert("Hello"); } );<p onsubmit="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first submitTrigger the select event of each matched element. This causes all of the functions that have been bound to thet select event to be executed.
jQuery
$("p").select();<p onselect="alert('Hello');">Hello</p>alert('Hello');Bind a function to the select event of each matched element.
jQuery
$("p").select( function() { alert("Hello"); } );<p>Hello</p>
<p onselect="alert('Hello');">Hello</p>Trigger the submit event of each matched element. This causes all of the functions that have been bound to thet submit event to be executed.
jQuery
$("p").submit();<p onsubmit="alert('Hello');">Hello</p>alert('Hello');Bind a function to the submit event of each matched element.
jQuery
$("p").submit( function() { alert("Hello"); } );<p>Hello</p>
<p onsubmit="alert('Hello');">Hello</p>Removes all bound change events from each of the matched elements.
jQuery
$("p").unchange();<p onchange="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound change event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unchange( myFunction );<p onchange="myFunction">Hello</p>
<p>Hello</p>
Removes all bound select events from each of the matched elements.
jQuery
$("p").unselect();<p onselect="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound select event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unselect( myFunction );<p onselect="myFunction">Hello</p>
<p>Hello</p>
Removes all bound submit events from each of the matched elements.
jQuery
$("p").unsubmit();<p onsubmit="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound submit event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unsubmit( myFunction );<p onsubmit="myFunction">Hello</p>
<p>Hello</p>
Trigger the keydown event of each matched element. This causes all of the functions that have been bound to thet keydown event to be executed.
jQuery
$("p").keydown();<p onkeydown="alert('Hello');">Hello</p>alert('Hello');Bind a function to the keydown event of each matched element.
jQuery
$("p").keydown( function() { alert("Hello"); } );<p>Hello</p>
<p onkeydown="alert('Hello');">Hello</p>Trigger the keypress event of each matched element. This causes all of the functions that have been bound to thet keypress event to be executed.
jQuery
$("p").keypress();<p onkeypress="alert('Hello');">Hello</p>alert('Hello');Bind a function to the keypress event of each matched element.
jQuery
$("p").keypress( function() { alert("Hello"); } );<p>Hello</p>
<p onkeypress="alert('Hello');">Hello</p>Trigger the keyup event of each matched element. This causes all of the functions that have been bound to thet keyup event to be executed.
jQuery
$("p").keyup();<p onkeyup="alert('Hello');">Hello</p>alert('Hello');Bind a function to the keyup event of each matched element.
jQuery
$("p").keyup( function() { alert("Hello"); } );<p>Hello</p>
<p onkeyup="alert('Hello');">Hello</p>Bind a function to the keydown event of each matched element, which will only be executed once. Unlike a call to the normal .keydown() method, calling .onekeydown() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onekeydown( function() { alert("Hello"); } );<p onkeydown="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first keydownBind a function to the keypress event of each matched element, which will only be executed once. Unlike a call to the normal .keypress() method, calling .onekeypress() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onekeypress( function() { alert("Hello"); } );<p onkeypress="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first keypressBind a function to the keyup event of each matched element, which will only be executed once. Unlike a call to the normal .keyup() method, calling .onekeyup() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onekeyup( function() { alert("Hello"); } );<p onkeyup="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first keyupRemoves all bound keydown events from each of the matched elements.
jQuery
$("p").unkeydown();<p onkeydown="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound keydown event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unkeydown( myFunction );<p onkeydown="myFunction">Hello</p>
<p>Hello</p>
Removes all bound keypress events from each of the matched elements.
jQuery
$("p").unkeypress();<p onkeypress="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound keypress event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unkeypress( myFunction );<p onkeypress="myFunction">Hello</p>
<p>Hello</p>
Removes all bound keyup events from each of the matched elements.
jQuery
$("p").unkeyup();<p onkeyup="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound keyup event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unkeyup( myFunction );<p onkeyup="myFunction">Hello</p>
<p>Hello</p>
Trigger the click event of each matched element. This causes all of the functions that have been bound to thet click event to be executed.
jQuery
$("p").click();<p onclick="alert('Hello');">Hello</p>alert('Hello');Bind a function to the click event of each matched element.
jQuery
$("p").click( function() { alert("Hello"); } );<p>Hello</p>
<p onclick="alert('Hello');">Hello</p>Trigger the dblclick event of each matched element. This causes all of the functions that have been bound to thet dblclick event to be executed.
jQuery
$("p").dblclick();<p ondblclick="alert('Hello');">Hello</p>alert('Hello');Bind a function to the dblclick event of each matched element.
jQuery
$("p").dblclick( function() { alert("Hello"); } );<p>Hello</p>
<p ondblclick="alert('Hello');">Hello</p>Trigger the mousedown event of each matched element. This causes all of the functions that have been bound to thet mousedown event to be executed.
jQuery
$("p").mousedown();<p onmousedown="alert('Hello');">Hello</p>alert('Hello');Bind a function to the mousedown event of each matched element.
jQuery
$("p").mousedown( function() { alert("Hello"); } );<p>Hello</p>
<p onmousedown="alert('Hello');">Hello</p>Trigger the mousemove event of each matched element. This causes all of the functions that have been bound to thet mousemove event to be executed.
jQuery
$("p").mousemove();<p onmousemove="alert('Hello');">Hello</p>alert('Hello');Bind a function to the mousemove event of each matched element.
jQuery
$("p").mousemove( function() { alert("Hello"); } );<p>Hello</p>
<p onmousemove="alert('Hello');">Hello</p>Trigger the mouseout event of each matched element. This causes all of the functions that have been bound to thet mouseout event to be executed.
jQuery
$("p").mouseout();<p onmouseout="alert('Hello');">Hello</p>alert('Hello');Bind a function to the mouseout event of each matched element.
jQuery
$("p").mouseout( function() { alert("Hello"); } );<p>Hello</p>
<p onmouseout="alert('Hello');">Hello</p>Trigger the mouseover event of each matched element. This causes all of the functions that have been bound to thet mousedown event to be executed.
jQuery
$("p").mouseover();<p onmouseover="alert('Hello');">Hello</p>alert('Hello');Bind a function to the mouseover event of each matched element.
jQuery
$("p").mouseover( function() { alert("Hello"); } );<p>Hello</p>
<p onmouseover="alert('Hello');">Hello</p>Trigger the mouseup event of each matched element. This causes all of the functions that have been bound to thet mouseup event to be executed.
jQuery
$("p").mouseup();<p onmouseup="alert('Hello');">Hello</p>alert('Hello');Bind a function to the mouseup event of each matched element.
jQuery
$("p").mouseup( function() { alert("Hello"); } );<p>Hello</p>
<p onmouseup="alert('Hello');">Hello</p>Bind a function to the click event of each matched element, which will only be executed once. Unlike a call to the normal .click() method, calling .oneclick() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").oneclick( function() { alert("Hello"); } );<p onclick="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first clickBind a function to the dblclick event of each matched element, which will only be executed once. Unlike a call to the normal .dblclick() method, calling .onedblclick() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onedblclick( function() { alert("Hello"); } );<p ondblclick="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first dblclickBind a function to the mousedown event of each matched element, which will only be executed once. Unlike a call to the normal .mousedown() method, calling .onemousedown() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onemousedown( function() { alert("Hello"); } );<p onmousedown="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first mousedownBind a function to the mousemove event of each matched element, which will only be executed once. Unlike a call to the normal .mousemove() method, calling .onemousemove() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onemousemove( function() { alert("Hello"); } );<p onmousemove="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first mousemoveBind a function to the mouseout event of each matched element, which will only be executed once. Unlike a call to the normal .mouseout() method, calling .onemouseout() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onemouseout( function() { alert("Hello"); } );<p onmouseout="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first mouseoutBind a function to the mouseover event of each matched element, which will only be executed once. Unlike a call to the normal .mouseover() method, calling .onemouseover() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onemouseover( function() { alert("Hello"); } );<p onmouseover="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first mouseoverBind a function to the mouseup event of each matched element, which will only be executed once. Unlike a call to the normal .mouseup() method, calling .onemouseup() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onemouseup( function() { alert("Hello"); } );<p onmouseup="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first mouseupRemoves all bound click events from each of the matched elements.
jQuery
$("p").unclick();<p onclick="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound click event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unclick( myFunction );<p onclick="myFunction">Hello</p>
<p>Hello</p>
Removes all bound dblclick events from each of the matched elements.
jQuery
$("p").undblclick();<p ondblclick="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound dblclick event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").undblclick( myFunction );<p ondblclick="myFunction">Hello</p>
<p>Hello</p>
Removes all bound mousedown events from each of the matched elements.
jQuery
$("p").unmousedown();<p onmousedown="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound mousedown event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unmousedown( myFunction );<p onmousedown="myFunction">Hello</p>
<p>Hello</p>
Removes all bound mousemove events from each of the matched elements.
jQuery
$("p").unmousemove();<p onmousemove="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound mousemove event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unmousemove( myFunction );<p onmousemove="myFunction">Hello</p>
<p>Hello</p>
Removes all bound mouseout events from each of the matched elements.
jQuery
$("p").unmouseout();<p onmouseout="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound mouseout event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unmouseout( myFunction );<p onmouseout="myFunction">Hello</p>
<p>Hello</p>
Removes all bound mouseover events from each of the matched elements.
jQuery
$("p").unmouseover();<p onmouseover="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound mouseover event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unmouseover( myFunction );<p onmouseover="myFunction">Hello</p>
<p>Hello</p>
Removes all bound mouseup events from each of the matched elements.
jQuery
$("p").unmouseup();<p onmouseup="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound mouseup event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unmouseup( myFunction );<p onmouseup="myFunction">Hello</p>
<p>Hello</p>
Trigger the blur event of each matched element. This causes all of the functions that have been bound to thet blur event to be executed.
jQuery
$("p").blur();<p onblur="alert('Hello');">Hello</p>alert('Hello');Bind a function to the blur event of each matched element.
jQuery
$("p").blur( function() { alert("Hello"); } );<p>Hello</p>
<p onblur="alert('Hello');">Hello</p>Trigger the focus event of each matched element. This causes all of the functions that have been bound to thet focus event to be executed.
jQuery
$("p").focus();<p onfocus="alert('Hello');">Hello</p>alert('Hello');Bind a function to the focus event of each matched element.
jQuery
$("p").focus( function() { alert("Hello"); } );<p>Hello</p>
<p onfocus="alert('Hello');">Hello</p>Bind a function to the blur event of each matched element, which will only be executed once. Unlike a call to the normal .blur() method, calling .oneblur() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").oneblur( function() { alert("Hello"); } );<p onblur="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first blurBind a function to the focus event of each matched element, which will only be executed once. Unlike a call to the normal .focus() method, calling .onefocus() causes the bound function to be only executed the first time it is triggered, and never again (unless it is re-bound).
jQuery
$("p").onefocus( function() { alert("Hello"); } );<p onfocus="alert('Hello');">Hello</p>alert('Hello'); // Only executed for the first focusRemoves all bound blur events from each of the matched elements.
jQuery
$("p").unblur();<p onblur="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound blur event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unblur( myFunction );<p onblur="myFunction">Hello</p>
<p>Hello</p>
Removes all bound focus events from each of the matched elements.
jQuery
$("p").unfocus();<p onfocus="alert('Hello');">Hello</p><p>Hello</p>
Removes a bound focus event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unfocus( myFunction );<p onfocus="myFunction">Hello</p>
<p>Hello</p>
Binds a handler to a particular event (like click) for each matched element. 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 has to return false.
jQuery
$("p").bind( "click", function() {
alert( $(this).text() );
} )<p>Hello</p>
alert("Hello")Cancel a default action and prevent it from bubbling by returning false from your function.
$("form").bind( "submit", function() { return false; } )Cancel only the default action by using the preventDefault method.
$("form").bind( "submit", function(event) {
event.preventDefault();
} );Stop only an event from bubbling by using the stopPropagation method.
$("form").bind( "submit", function(event) {
event.stopPropagation();
} )A method for simulating hovering (moving the mouse on, and off, an object). This is a custom method which provides an 'in' to a frequent task.
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).
jQuery
$("p").hover(function(){
$(this).addClass("over");
},function(){
$(this).addClass("out");
});Bind a function to be executed whenever the DOM is ready to be traversed and manipulated. This is probably the most important function included in the event module, as it can greatly improve the response times of your web applications.
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 exactly what 99.99% of all Javascript code needs to run.
Please ensure you have no code in your <body> onload event handler, otherwise $(document).ready() may not fire.
You can have as many $(document).ready events on your page as you like.
jQuery
$(document).ready(function(){ Your code here... });Toggle between two function calls every other click. Whenever a matched element is clicked, the first specified function is fired, when clicked again, the second is fired. All subsequent clicks continue to rotate through the two functions.
jQuery
$("p").toggle(function(){
$(this).addClass("selected");
},function(){
$(this).removeClass("selected");
});Trigger a type of event on every matched element.
jQuery
$("p").trigger("click")<p click="alert('hello')">Hello</p>alert('hello')Removes all bound events from each of the matched elements.
jQuery
$("p").unbind()<p onclick="alert('Hello');">Hello</p>[ <p>Hello</p> ]
Removes all bound events of a particular type from each of the matched elements.
jQuery
$("p").unbind( "click" )<p onclick="alert('Hello');">Hello</p>[ <p>Hello</p> ]
The opposite of bind, removes a bound event from each of the matched elements. You must pass the identical function that was used in the original bind method.
jQuery
$("p").unbind( "click", function() { alert("Hello"); } )<p onclick="alert('Hello');">Hello</p>[ <p>Hello</p> ]
Load a remote page using an HTTP request. This function is the primary means of making AJAX requests using jQuery. $.ajax() takes one property, an object of key/value pairs, that're are used to initalize the request.
These are all the key/values that can be passed in to 'prop':
(String) type - The type of request to make (e.g. "POST" or "GET").
(String) url - The URL of the page to request.
(String) data - A string of data to be sent to the server (POST only).
(String) dataType - The type of data that you're expecting back from the server (e.g. "xml", "html", "script", or "json").
(Boolean) ifModified - Allow the request to be successful only if the response has changed since the last request, default is false, ignoring the Last-Modified header
(Number) timeout - Local timeout to override global timeout, eg. to give a single request a longer timeout while all others timeout after 1 seconds, see $.ajaxTimeout
(Boolean) global - Wheather to trigger global AJAX event handlers for this request, default is true. Set to true to prevent that global handlers like ajaxStart or ajaxStop are triggered.
(Function) error - A function to be called if the request fails. The function gets passed two arguments: The XMLHttpRequest object and a string describing the type of error that occurred.
(Function) success - A function to be called if the request succeeds. The function gets passed one argument: The data returned from the server, formatted according to the 'dataType' parameter.
(Function) complete - A function to be called when the request finishes. The function gets passed two arguments: The XMLHttpRequest object and a string describing the type the success of the request.
jQuery
Load and execute a JavaScript file.
$.ajax({
type: "GET",
url: "test.js",
dataType: "script"
})Save some data to the server and notify the user once its complete.
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});Set the timeout of all AJAX requests to a specific amount of time. This will make all future AJAX requests timeout after a specified amount of time (the default is no timeout).
jQuery
Make all AJAX requests timeout after 5 seconds.
$.ajaxTimeout( 5000 );
Load a remote page using an HTTP GET request. All of the arguments to the method (except URL) are optional.
jQuery
$.get("test.cgi")$.get("test.cgi", { name: "John", time: "2pm" } )$.get("test.cgi", function(data){
alert("Data Loaded: " + data);
})$.get("test.cgi",
{ name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
}
)Load a remote page using an HTTP GET request, only if it hasn't been modified since it was last retrieved. All of the arguments to the method (except URL) are optional.
jQuery
$.getIfModified("test.html")$.getIfModified("test.html", { name: "John", time: "2pm" } )$.getIfModified("test.cgi", function(data){
alert("Data Loaded: " + data);
})$.getifModified("test.cgi",
{ name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
}
)Load a remote JSON object using an HTTP GET request. All of the arguments to the method (except URL) are optional.
jQuery
$.getJSON("test.js", function(json){
alert("JSON Data: " + json.users[3].name);
})$.getJSON("test.js",
{ name: "John", time: "2pm" },
function(json){
alert("JSON Data: " + json.users[3].name);
}
)Loads, and executes, a remote JavaScript file using an HTTP GET request. All of the arguments to the method (except URL) are optional.
jQuery
$.getScript("test.js")$.getScript("test.js", function(){
alert("Script loaded and executed.");
})Load a remote page using an HTTP POST request. All of the arguments to the method (except URL) are optional.
jQuery
$.post("test.cgi")$.post("test.cgi", { name: "John", time: "2pm" } )$.post("test.cgi", function(data){
alert("Data Loaded: " + data);
})$.post("test.cgi",
{ name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
}
)Attach a function to be executed whenever an AJAX request completes.
jQuery
Show a message when an AJAX request completes.
$("#msg").ajaxComplete(function(){
$(this).append("<li>Request Complete.</li>");
});Attach a function to be executed whenever an AJAX request fails.
jQuery
Show a message when an AJAX request fails.
$("#msg").ajaxError(function(){
$(this).append("<li>Error requesting page.</li>");
});Attach a function to be executed whenever an AJAX request begins.
jQuery
Show a loading message whenever an AJAX request starts.
$("#loading").ajaxStart(function(){
$(this).show();
});Attach a function to be executed whenever all AJAX requests have ended.
jQuery
Hide a loading message after all the AJAX requests have stopped.
$("#loading").ajaxStop(function(){
$(this).hide();
});Attach a function to be executed whenever an AJAX request completes successfully.
jQuery
Show a message when an AJAX request completes successfully.
$("#msg").ajaxSuccess(function(){
$(this).append("<li>Successful Request!</li>");
});Load HTML from a remote file and inject it into the DOM.
jQuery
$("#feeds").load("feeds.html")<div id="feeds"></div>
<div id="feeds"><b>45</b> feeds found.</div>
Same as above, but with an additional parameter and a callback that is executed when the data was loaded.
$("#feeds").load("feeds.html",
{test: true},
function() { alert("load is done"); }
);Load HTML from a remote file and inject it into the DOM, only if it's been modified by the server.
jQuery
$("#feeds").loadIfModified("feeds.html")<div id="feeds"></div>
<div id="feeds"><b>45</b> feeds found.</div>
Serializes a set of input elements into a string of data. This will serialize all given elements. If you need serialization similar to the form submit of a browser, you should use the form plugin. This is also true for selects with multiple attribute set, only a single option is serialized.
String
Serialize a selection of input elements to a string
$("input[@type=text]").serialize();<input type='text' name='name' value='John'/> <input type='text' name='location' value='Boston'/>