Core
$(html)

$(html)

Create DOM elements on-the-fly from the provided String of raw HTML.

Returns

jQuery

Parameters

  • html (String): A string of HTML to create on the fly.

Example

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.

jQuery Code

$("<div><p>Hello</p></div>").appendTo("body")
$(elems)

$(elems)

Wrap jQuery functionality around a single or multiple DOM Element(s).

This function also accepts XML Documents and Window objects as valid arguments (even though they are not DOM Elements).

Returns

jQuery

Parameters

  • elems (Element|Array<Element>): DOM element(s) to be encapsulated by a jQuery object.

Example

Sets the background color of the page to black.

jQuery Code

$(document.body).css( "background", "black" );

Example

Hides all the input elements within a form

jQuery Code

$( myForm.elements ).hide()
$(fn)

$(fn)

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 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.

Returns

jQuery

Parameters

  • fn (Function): The function to execute when the DOM is ready.

Example

Executes the function when the DOM is ready to be used.

jQuery Code

$(function(){
  // Document is ready
});

Example

Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.

jQuery Code

jQuery(function($) {
  // Your code using failsafe $ alias here...
});
$(expr, context)

$(expr, context)

This function accepts a string containing a CSS or basic XPath selector which is then used to match a set of elements.

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.

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 [[DOM/Traversing/Selectors]] for the allowed CSS/XPath syntax for expressions.

Returns

jQuery

Parameters

  • expr (String): An expression to search with
  • context (Element|jQuery): (optional) A DOM Element, Document or jQuery to use as context

Example

Finds all p elements that are children of a div element.

jQuery Code

$("div > p")

Before

<p>one</p> <div><p>two</p></div> <p>three</p>

Result:

[ <p>two</p> ]

Example

Searches for all inputs of type radio within the first form in the document

jQuery Code

$("input:radio", document.forms[0])

Example

This finds all div elements within the specified XML document.

jQuery Code

$("div", xml.responseXML)
$.extend(prop)

$.extend(prop)

Extends the jQuery object itself. Can be used to add functions into the jQuery namespace and to [[Plugins/Authoring|add plugin methods]] (plugins).

Returns

Object

Parameters

  • prop (Object): The object that will be merged into the jQuery object

Example

Adds two plugin methods.

jQuery Code

jQuery.fn.extend({
  check: function() {
    return this.each(function() { this.checked = true; });
  },
  uncheck: function() {
    return this.each(function() { this.checked = false; });
  }
});
$("input[@type=checkbox]").check();
$("input[@type=radio]").uncheck();

Example

Adds two functions into the jQuery namespace

jQuery Code

jQuery.extend({
  min: function(a, b) { return a < b ? a : b; },
  max: function(a, b) { return a > b ? a : b; }
});
$.noConflict()

$.noConflict()

Run this function to give control of the $ variable back to whichever library first implemented it. 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").

Returns

undefined

Example

Maps the original object that was referenced by $ back to $

jQuery Code

jQuery.noConflict();
// Do something with jQuery
jQuery("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';

Example

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.

jQuery Code

jQuery.noConflict();
(function($) { 
  $(function() {
    // more code using $ as alias to jQuery
  });
})(jQuery);
// other code using $ as an alias to the other library
each(fn)

each(fn)

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 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).

Returns

jQuery

Parameters

  • fn (Function): A function to execute

Example

Iterates over two images and sets their src property

jQuery Code

$("img").each(function(i){
  this.src = "test" + i + ".jpg";
});

Before

<img/><img/>

Result:

<img src="test0.jpg"/><img src="test1.jpg"/>
eq(pos)

eq(pos)

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.

Returns

jQuery

Parameters

  • pos (Number): The index of the element that you wish to limit to.

Example

jQuery Code

$("p").eq(1)

Before

<p>This is just a test.</p><p>So is this</p>

Result:

[ <p>So is this</p> ]
get()

get()

Access all matched DOM 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).

It is useful if you need to operate on the DOM elements themselves instead of using built-in jQuery functions.

Returns

Array<Element>

Example

Selects all images in the document and returns the DOM Elements as an Array

jQuery Code

$("img").get();

Before

<img src="test1.jpg"/> <img src="test2.jpg"/>

Result:

[ <img src="test1.jpg"/> <img src="test2.jpg"/> ]
get(num)

get(num)

Access a single matched DOM element at a specified index in the matched set. This allows you to extract the actual DOM element and operate on it directly without necessarily using jQuery functionality on it.

Returns

Element

Parameters

  • num (Number): Access the element in the Nth position.

Example

Selects all images in the document and returns the first one

jQuery Code

$("img").get(0);

Before

<img src="test1.jpg"/> <img src="test2.jpg"/>

Result:

<img src="test1.jpg"/>
gt(pos)

gt(pos)

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.

Returns

jQuery

Parameters

  • pos (Number): Reduce the set to all elements after this position.

Example

jQuery Code

$("p").gt(0)

Before

<p>This is just a test.</p><p>So is this</p>

Result:

[ <p>So is this</p> ]
index(subject)

index(subject)

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.

Returns

Number

Parameters

  • subject (Element): Object to search for

Example

Returns the index for the element with ID foobar

jQuery Code

$("*").index( $('#foobar')[0] )

Before

<div id="foobar"><b></b><span id="foo"></span></div>

Result:

0

Example

Returns the index for the element with ID foo within another element

jQuery Code

$("*").index( $('#foo')[0] )

Before

<div id="foobar"><b></b><span id="foo"></span></div>

Result:

2

Example

Returns -1, as there is no element with ID bar

jQuery Code

$("*").index( $('#bar')[0] )

Before

<div id="foobar"><b></b><span id="foo"></span></div>

Result:

-1
length

length

The number of elements currently matched. The size function will return the same value.

Returns

Number

Example

jQuery Code

$("img").length;

Before

<img src="test1.jpg"/> <img src="test2.jpg"/>

Result:

2
lt(pos)

lt(pos)

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.

Returns

jQuery

Parameters

  • pos (Number): Reduce the set to all elements below this position.

Example

jQuery Code

$("p").lt(1)

Before

<p>This is just a test.</p><p>So is this</p>

Result:

[ <p>This is just a test.</p> ]
size()

size()

Get the number of elements currently matched. This returns the same number as the 'length' property of the jQuery object.

Returns

Number

Example

jQuery Code

$("img").size();

Before

<img src="test1.jpg"/> <img src="test2.jpg"/>

Result:

2
DOM
Attributes
addClass(class)

addClass(class)

Adds the specified class(es) to each of the set of matched elements.

Returns

jQuery

Parameters

  • class (String): One or more CSS classes to add to the elements

Example

jQuery Code

$("p").addClass("selected")

Before

<p>Hello</p>

Result:

[ <p class="selected">Hello</p> ]

Example

jQuery Code

$("p").addClass("selected highlight")

Before

<p>Hello</p>

Result:

[ <p class="selected highlight">Hello</p> ]
attr(name)

attr(name)

Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element.

If the element does not have an attribute with such a name, undefined is returned.

Returns

Object

Parameters

  • name (String): The name of the property to access.

Example

Returns the src attribute from the first image in the document.

jQuery Code

$("img").attr("src");

Before

<img src="test.jpg"/>

Result:

test.jpg
attr(properties)

attr(properties)

Set a key/value object as properties to all matched elements.

This serves as the best way to set a large number of properties on all matched elements.

Returns

jQuery

Parameters

  • properties (Map): Key/value pairs to set as object properties.

Example

Sets src and alt attributes to all images.

jQuery Code

$("img").attr({ src: "test.jpg", alt: "Test Image" });

Before

<img/>

Result:

<img src="test.jpg" alt="Test Image"/>
attr(key, value)

attr(key, value)

Set a single property to a value, on all matched elements.

Note that you can't set the name property of input elements in IE. Use $(html) or .append(html) or .html(html) to create elements on the fly including the name property.

Returns

jQuery

Parameters

  • key (String): The name of the property to set.
  • value (Object): The value to set the property to.

Example

Sets src attribute to all images.

jQuery Code

$("img").attr("src","test.jpg");

Before

<img/>

Result:

<img src="test.jpg"/>
attr(key, value)

attr(key, value)

Set a single property to a computed value, on all matched elements.

Instead of supplying a string value as described [[DOM/Attributes#attr.28_key.2C_value_.29|above]], a function is provided that computes the value.

Returns

jQuery

Parameters

  • key (String): The name of the property to set.
  • value (Function): A function returning the value to set. Scope: Current element, argument: Index of current element

Example

Sets title attribute from src attribute.

jQuery Code

$("img").attr("title", function() { return this.src });

Before

<img src="test.jpg" />

Result:

<img src="test.jpg" title="test.jpg" />

Example

Enumerate title attribute.

jQuery Code

$("img").attr("title", function(index) { return this.title + (i + 1); });

Before

<img title="pic" /><img title="pic" /><img title="pic" />

Result:

<img title="pic1" /><img title="pic2" /><img title="pic3" />
html()

html()

Get the html contents of the first matched element. This property is not available on XML documents.

Returns

String

Example

jQuery Code

$("div").html();

Before

<div><input/></div>

Result:

<input/>
html(val)

html(val)

Set the html contents of every matched element. This property is not available on XML documents.

Returns

jQuery

Parameters

  • val (String): Set the html contents to the specified value.

Example

jQuery Code

$("div").html("<b>new stuff</b>");

Before

<div><input/></div>

Result:

<div><b>new stuff</b></div>
removeAttr(name)

removeAttr(name)

Remove an attribute from each of the matched elements.

Returns

jQuery

Parameters

  • name (String): The name of the attribute to remove.

Example

jQuery Code

$("input").removeAttr("disabled")

Before

<input disabled="disabled"/>

Result:

<input/>
removeClass(class)

removeClass(class)

Removes all or the specified class(es) from the set of matched elements.

Returns

jQuery

Parameters

  • class (String): (optional) One or more CSS classes to remove from the elements

Example

jQuery Code

$("p").removeClass()

Before

<p class="selected">Hello</p>

Result:

[ <p>Hello</p> ]

Example

jQuery Code

$("p").removeClass("selected")

Before

<p class="selected first">Hello</p>

Result:

[ <p class="first">Hello</p> ]

Example

jQuery Code

$("p").removeClass("selected highlight")

Before

<p class="highlight selected first">Hello</p>

Result:

[ <p class="first">Hello</p> ]
text()

text()

Get 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.

Returns

String

Example

Gets the concatenated text of all paragraphs

jQuery Code

$("p").text();

Before

<p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>

Result:

Test Paragraph.Paraparagraph
text(val)

text(val)

Set the text contents of all matched elements.

Similar to html(), but escapes HTML (replace "<" and ">" with their HTML entities).

Returns

String

Parameters

  • val (String): The text value to set the contents of the element to.

Example

Sets the text of all paragraphs.

jQuery Code

$("p").text("<b>Some</b> new text.");

Before

<p>Test Paragraph.</p>

Result:

<p>&lt;b&gt;Some&lt;/b&gt; new text.</p>

Example

Sets the text of all paragraphs.

jQuery Code

$("p").text("<b>Some</b> new text.", true);

Before

<p>Test Paragraph.</p>

Result:

<p>Some new text.</p>
toggleClass(class)

toggleClass(class)

Adds the specified class if it is not present, removes it if it is present.

Returns

jQuery

Parameters

  • class (String): A CSS class with which to toggle the elements

Example

jQuery Code

$("p").toggleClass("selected")

Before

<p>Hello</p><p class="selected">Hello Again</p>

Result:

[ <p class="selected">Hello</p>, <p>Hello Again</p> ]
val()

val()

Get the content of the value attribute of the first matched element.

Use caution when relying on this function to check the value of multiple-select elements and checkboxes in a form. While it will still work as intended, it may not accurately represent the value the server will receive because these elements may send an array of values. For more robust handling of field values, see the [http://www.malsup.com/jquery/form/#fields fieldValue function of the Form Plugin].

Returns

String

Example

jQuery Code

$("input").val();

Before

<input type="text" value="some text"/>

Result:

"some text"
val(val)

val(val)

Set the value attribute of every matched element.

Returns

jQuery

Parameters

  • val (String): Set the property to the specified value.

Example

jQuery Code

$("input").val("test");

Before

<input type="text" value="some text"/>

Result:

<input type="text" value="test"/>
Manipulation
after(content)

after(content)

Insert content after each of the matched elements.

Returns

jQuery

Parameters

  • content (<Content>): Content to insert after each target.

Example

Inserts some HTML after all paragraphs.

jQuery Code

$("p").after("<b>Hello</b>");

Before

<p>I would like to say: </p>

Result:

<p>I would like to say: </p><b>Hello</b>

Example

Inserts an Element after all paragraphs.

jQuery Code

$("p").after( $("#foo")[0] );

Before

<b id="foo">Hello</b><p>I would like to say: </p>

Result:

<p>I would like to say: </p><b id="foo">Hello</b>

Example

Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs.

jQuery Code

$("p").after( $("b") );

Before

<b>Hello</b><p>I would like to say: </p>

Result:

<p>I would like to say: </p><b>Hello</b>
append(content)

append(content)

Append content to the inside of every matched element.

This operation is similar to doing an appendChild to all the specified elements, adding them into the document.

Returns

jQuery

Parameters

  • content (<Content>): Content to append to the target

Example

Appends some HTML to all paragraphs.

jQuery Code

$("p").append("<b>Hello</b>");

Before

<p>I would like to say: </p>

Result:

<p>I would like to say: <b>Hello</b></p>

Example

Appends an Element to all paragraphs.

jQuery Code

$("p").append( $("#foo")[0] );

Before

<p>I would like to say: </p><b id="foo">Hello</b>

Result:

<p>I would like to say: <b id="foo">Hello</b></p>

Example

Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.

jQuery Code

$("p").append( $("b") );

Before

<p>I would like to say: </p><b>Hello</b>

Result:

<p>I would like to say: <b>Hello</b></p>
appendTo(content)

appendTo(content)

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.

Returns

jQuery

Parameters

  • content (<Content>): Content to append to the selected element to.

Example

Appends all paragraphs to the element with the ID "foo"

jQuery Code

$("p").appendTo("#foo");

Before

<p>I would like to say: </p><div id="foo"></div>

Result:

<div id="foo"><p>I would like to say: </p></div>
before(content)

before(content)

Insert content before each of the matched elements.

Returns

jQuery

Parameters

  • content (<Content>): Content to insert before each target.

Example

Inserts some HTML before all paragraphs.

jQuery Code

$("p").before("<b>Hello</b>");

Before

<p>I would like to say: </p>

Result:

<b>Hello</b><p>I would like to say: </p>

Example

Inserts an Element before all paragraphs.

jQuery Code

$("p").before( $("#foo")[0] );

Before

<p>I would like to say: </p><b id="foo">Hello</b>

Result:

<b id="foo">Hello</b><p>I would like to say: </p>

Example

Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs.

jQuery Code

$("p").before( $("b") );

Before

<p>I would like to say: </p><b>Hello</b>

Result:

<b>Hello</b><p>I would like to say: </p>
clone(deep)

clone(deep)

Clone matched DOM Elements and select the clones.

This is useful for moving copies of the elements to another location in the DOM.

Returns

jQuery

Parameters

  • deep (Boolean): (Optional) Set to false if you don't want to clone all descendant nodes, in addition to the element itself.

Example

Clones all b elements (and selects the clones) and prepends them to all paragraphs.

jQuery Code

$("b").clone().prependTo("p");

Before

<b>Hello</b><p>, how are you?</p>

Result:

<b>Hello</b><p><b>Hello</b>, how are you?</p>
empty()

empty()

Removes all child nodes from the set of matched elements.

Returns

jQuery

Example

jQuery Code

$("p").empty()

Before

<p>Hello, <span>Person</span> <a href="#">and person</a></p>

Result:

[ <p></p> ]
insertAfter(content)

insertAfter(content)

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.

Returns

jQuery

Parameters

  • content (<Content>): Content to insert the selected element after.

Example

Same as $("#foo").after("p")

jQuery Code

$("p").insertAfter("#foo");

Before

<p>I would like to say: </p><div id="foo">Hello</div>

Result:

<div id="foo">Hello</div><p>I would like to say: </p>
insertBefore(content)

insertBefore(content)

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.

Returns

jQuery

Parameters

  • content (<Content>): Content to insert the selected element before.

Example

Same as $("#foo").before("p")

jQuery Code

$("p").insertBefore("#foo");

Before

<div id="foo">Hello</div><p>I would like to say: </p>

Result:

<p>I would like to say: </p><div id="foo">Hello</div>
prepend(content)

prepend(content)

Prepend content to the inside of every matched element.

This operation is the best way to insert elements inside, at the beginning, of all matched elements.

Returns

jQuery

Parameters

  • content (<Content>): Content to prepend to the target.

Example

Prepends some HTML to all paragraphs.

jQuery Code

$("p").prepend("<b>Hello</b>");

Before

<p>I would like to say: </p>

Result:

<p><b>Hello</b>I would like to say: </p>

Example

Prepends an Element to all paragraphs.

jQuery Code

$("p").prepend( $("#foo")[0] );

Before

<p>I would like to say: </p><b id="foo">Hello</b>

Result:

<p><b id="foo">Hello</b>I would like to say: </p>

Example

Prepends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.

jQuery Code

$("p").prepend( $("b") );

Before

<p>I would like to say: </p><b>Hello</b>

Result:

<p><b>Hello</b>I would like to say: </p>
prependTo(content)

prependTo(content)

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.

Returns

jQuery

Parameters

  • content (<Content>): Content to prepend to the selected element to.

Example

Prepends all paragraphs to the element with the ID "foo"

jQuery Code

$("p").prependTo("#foo");

Before

<p>I would like to say: </p><div id="foo"><b>Hello</b></div>

Result:

<div id="foo"><p>I would like to say: </p><b>Hello</b></div>
remove(expr)

remove(expr)

Removes all matched elements from the DOM. This does NOT remove them from the jQuery object, allowing you to use the matched elements further.

Can be filtered with an optional expressions.

Returns

jQuery

Parameters

  • expr (String): (optional) A jQuery expression to filter elements by.

Example

jQuery Code

$("p").remove();

Before

<p>Hello</p> how are <p>you?</p>

Result:

how are

Example

jQuery Code

$("p").remove(".hello");

Before

<p class="hello">Hello</p> how are <p>you?</p>

Result:

how are <p>you?</p>
wrap(html)

wrap(html)

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.

Returns

jQuery

Parameters

  • html (String): A string of HTML, that will be created on the fly and wrapped around the target.

Example

jQuery Code

$("p").wrap("<div class='wrap'></div>");

Before

<p>Test Paragraph.</p>

Result:

<div class='wrap'><p>Test Paragraph.</p></div>
wrap(elem)

wrap(elem)

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.

Returns

jQuery

Parameters

  • elem (Element): A DOM element that will be wrapped around the target.

Example

jQuery Code

$("p").wrap( document.getElementById('content') );

Before

<p>Test Paragraph.</p><div id="content"></div>

Result:

<div id="content"><p>Test Paragraph.</p></div>
Traversing
add(expr)

add(expr)

Adds more elements, matched by the given expression, to the set of matched elements.

Returns

jQuery

Parameters

  • expr (String): An expression whose matched elements are added

Example

Compare the above result to the result of <code>$('p')</code>, which would just result in <code><nowiki>[ <p>Hello</p> ]</nowiki></code>. Using add(), matched elements of <code>$('span')</code> are simply added to the returned jQuery-object.

jQuery Code

$("p").add("span")

Before

(HTML) <p>Hello</p><span>Hello Again</span>

Result:

(jQuery object matching 2 elements) [ <p>Hello</p>, <span>Hello Again</span> ]
add(html)

add(html)

Adds more elements, created on the fly, to the set of matched elements.

Returns

jQuery

Parameters

  • html (String): A string of HTML to create on the fly.

Example

jQuery Code

$("p").add("<span>Again</span>")

Before

<p>Hello</p>

Result:

[ <p>Hello</p>, <span>Again</span> ]
add(elements)

add(elements)

Adds one or more Elements to the set of matched elements.

Returns

jQuery

Parameters

  • elements (Element|Array<Element>): One or more Elements to add

Example

jQuery Code

$("p").add( document.getElementById("a") )

Before

<p>Hello</p><p><span id="a">Hello Again</span></p>

Result:

[ <p>Hello</p>, <span id="a">Hello Again</span> ]

Example

jQuery Code

$("p").add( document.forms[0].elements )

Before

<p>Hello</p><p><form><input/><button/></form>

Result:

[ <p>Hello</p>, <input/>, <button/> ]
children(expr)

children(expr)

Get a set of elements containing all of the unique children of each of the matched set of elements.

This set can be filtered with an optional expression that will cause only elements matching the selector to be collected.

Returns

jQuery

Parameters

  • expr (String): (optional) An expression to filter the child Elements with

Example

Find all children of each div.

jQuery Code

$("div").children()

Before

<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>

Result:

[ <span>Hello Again</span> ]

Example

Find all children with a class "selected" of each div.

jQuery Code

$("div").children(".selected")

Before

<div><span>Hello</span><p class="selected">Hello Again</p><p>And Again</p></div>

Result:

[ <p class="selected">Hello Again</p> ]
contains(str)

contains(str)

Filter the set of elements to those that contain the specified text.

Returns

jQuery

Parameters

  • str (String): The string that will be contained within the text of an element.

Example

jQuery Code

$("p").contains("test")

Before

<p>This is just a test.</p><p>So is this</p>

Result:

[ <p>This is just a test.</p> ]
end()

end()

Revert the most recent 'destructive' operation, changing the set of matched elements to its previous state (right before the destructive operation).

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. These functions are: <code>add</code>, <code>children</code>, <code>clone</code>, <code>filter</code>, <code>find</code>, <code>not</code>, <code>next</code>, <code>parent</code>, <code>parents</code>, <code>prev</code> and <code>siblings</code>.

Returns

jQuery

Example

Selects all paragraphs, finds span elements inside these, and reverts the selection back to the paragraphs.

jQuery Code

$("p").find("span").end();

Before

<p><span>Hello</span>, how are you?</p>

Result:

[ <p>...</p> ]
filter(expression)

filter(expression)

Removes all elements from the set of matched elements that do not match the specified expression(s). 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.

Returns

jQuery

Parameters

  • expression (String): Expression(s) to search with.

Example

Selects all paragraphs and removes those without a class "selected".

jQuery Code

$("p").filter(".selected")

Before

<p class="selected">Hello</p><p>How are you?</p>

Result:

[ <p class="selected">Hello</p> ]

Example

Selects all paragraphs and removes those without class "selected" and being the first one.

jQuery Code

$("p").filter(".selected, :first")

Before

<p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>

Result:

[ <p>Hello</p>, <p class="selected">And Again</p> ]
filter(filter)

filter(filter)

Removes all elements from the set of matched elements that do not pass the specified filter. This method is used to narrow down the results of a search.

Returns

jQuery

Parameters

  • filter (Function): A function to use for filtering

Example

Remove all elements that have a child ol element

jQuery Code

$("p").filter(function(index) {
  return $("ol", this).length == 0;
})

Before

<p><ol><li>Hello</li></ol></p><p>How are you?</p>

Result:

[ <p>How are you?</p> ]
find(expr)

find(expr)

Searches for all elements that match the specified expression. This method is a good way to find 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.

Returns

jQuery

Parameters

  • expr (String): An expression to search with.

Example

Starts with all paragraphs and searches for descendant span elements, same as $("p span")

jQuery Code

$("p").find("span");

Before

<p><span>Hello</span>, how are you?</p>

Result:

[ <span>Hello</span> ]
is(expr)

is(expr)

Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.

Does return false, if no element fits or the expression is not valid.

filter(String) is used internally, therefore all rules that apply there apply here, too.

Returns

Boolean

Parameters

  • expr (String): The expression with which to filter

Example

Returns true, because the parent of the input is a form element

jQuery Code

$("input[@type='checkbox']").parent().is("form")

Before

<form><input type="checkbox" /></form>

Result:

true

Example

Returns false, because the parent of the input is a p element

jQuery Code

$("input[@type='checkbox']").parent().is("form")

Before

<form><p><input type="checkbox" /></p></form>

Result:

false
next(expr)

next(expr)

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 for each element, not all next siblings.

You may provide an optional expression to filter the match.

Returns

jQuery

Parameters

  • expr (String): (optional) An expression to filter the next Elements with

Example

Find the very next sibling of each paragraph.

jQuery Code

$("p").next()

Before

<p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>

Result:

[ <p>Hello Again</p>, <div><span>And Again</span></div> ]

Example

Find the very next sibling of each paragraph that has a class "selected".

jQuery Code

$("p").next(".selected")

Before

<p>Hello</p><p class="selected">Hello Again</p><div><span>And Again</span></div>

Result:

[ <p class="selected">Hello Again</p> ]
not(el)

not(el)

Removes the specified Element from the set of matched elements. This method is used to remove a single Element from a jQuery object.

Returns

jQuery

Parameters

  • el (Element): An element to remove from the set

Example

Removes the element with the ID "selected" from the set of all paragraphs.

jQuery Code

$("p").not( $("#selected")[0] )

Before

<p>Hello</p><p id="selected">Hello Again</p>

Result:

[ <p>Hello</p> ]
not(expr)

not(expr)

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.

Returns

jQuery

Parameters

  • expr (String): An expression with which to remove matching elements

Example

Removes the element with the ID "selected" from the set of all paragraphs.

jQuery Code

$("p").not("#selected")

Before

<p>Hello</p><p id="selected">Hello Again</p>

Result:

[ <p>Hello</p> ]
not(elems)

not(elems)

Removes any elements inside the array of elements from the set of matched elements. This method is used to remove one or more elements from a jQuery object.

Please note: the expression cannot use a reference to the element name. See the two examples below.

Returns

jQuery

Parameters

  • elems (jQuery): A set of elements to remove from the jQuery set of matched elements.

Example

Removes all elements that match "div p.selected" from the total set of all paragraphs.

jQuery Code

$("p").not( $("div p.selected") )

Before

<div><p>Hello</p><p class="selected">Hello Again</p></div>

Result:

[ <p>Hello</p> ]
parent(expr)

parent(expr)

Get a set of elements containing the unique parents of the matched set of elements.

You may use an optional expression to filter the set of parent elements that will match.

Returns

jQuery

Parameters

  • expr (String): (optional) An expression to filter the parents with

Example

Find the parent element of each paragraph.

jQuery Code

$("p").parent()

Before

<div><p>Hello</p><p>Hello</p></div>

Result:

[ <div><p>Hello</p><p>Hello</p></div> ]

Example

Find the parent element of each paragraph with a class "selected".

jQuery Code

$("p").parent(".selected")

Before

<div><p>Hello</p></div><div class="selected"><p>Hello Again</p></div>

Result:

[ <div class="selected"><p>Hello Again</p></div> ]
parents(expr)

parents(expr)

Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element).

The matched elements can be filtered with an optional expression.

Returns

jQuery

Parameters

  • expr (String): (optional) An expression to filter the ancestors with

Example

Find all parent elements of each span.

jQuery Code

$("span").parents()

Before

<html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>

Result:

[ <body>...</body>, <div>...</div>, <p><span>Hello</span></p> ]

Example

Find all parent elements of each span that is a paragraph.

jQuery Code

$("span").parents("p")

Before

<html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>

Result:

[ <p><span>Hello</span></p> ]
prev(expr)

prev(expr)

Get a set of elements containing the unique previous siblings of each of the matched set of elements.

Use an optional expression to filter the matched set.

Only the immediately previous sibling is returned, not all previous siblings.

Returns

jQuery

Parameters

  • expr (String): (optional) An expression to filter the previous Elements with

Example

Find the very previous sibling of each paragraph.

jQuery Code

$("p").prev()

Before

<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>

Result:

[ <div><span>Hello Again</span></div> ]

Example

Find the very previous sibling of each paragraph that has a class "selected".

jQuery Code

$("p").prev(".selected")

Before

<div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>

Result:

[ <div><span>Hello</span></div> ]
siblings(expr)

siblings(expr)

Get a set of elements containing all of the unique siblings of each of the matched set of elements.

Can be filtered with an optional expressions.

Returns

jQuery

Parameters

  • expr (String): (optional) An expression to filter the sibling Elements with

Example

Find all siblings of each div.

jQuery Code

$("div").siblings()

Before

<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>

Result:

[ <p>Hello</p>, <p>And Again</p> ]

Example

Find all siblings with a class "selected" of each div.

jQuery Code

$("div").siblings(".selected")

Before

<div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>

Result:

[ <p class="selected">Hello Again</p> ]
CSS
css(name)

css(name)

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.

Returns

String

Parameters

  • name (String): The name of the property to access.

Example

Retrieves the color style of the first paragraph

jQuery Code

$("p").css("color");

Before

<p style="color:red;">Test Paragraph.</p>

Result:

"red"

Example

Retrieves the font-weight style of the first paragraph.

jQuery Code

$("p").css("font-weight");

Before

<p style="font-weight: bold;">Test Paragraph.</p>

Result:

"bold"
css(properties)

css(properties)

Set a key/value object as style properties to all matched elements.

This serves as the best way to set a large number of style properties on all matched elements.

Returns

jQuery

Parameters

  • properties (Map): Key/value pairs to set as style properties.

Example

Sets color and background styles to all p elements.

jQuery Code

$("p").css({ color: "red", background: "blue" });

Before

<p>Test Paragraph.</p>

Result:

<p style="color:red; background:blue;">Test Paragraph.</p>
css(key, value)

css(key, value)

Set a single style property to a value, on all matched elements. If a number is provided, it is automatically converted into a pixel value.

Returns

jQuery

Parameters

  • key (String): The name of the property to set.
  • value (String|Number): The value to set the property to.

Example

Changes the color of all paragraphs to red

jQuery Code

$("p").css("color","red");

Before

<p>Test Paragraph.</p>

Result:

<p style="color:red;">Test Paragraph.</p>

Example

Changes the left of all paragraphs to "30px"

jQuery Code

$("p").css("left",30);

Before

<p>Test Paragraph.</p>

Result:

<p style="left:30px;">Test Paragraph.</p>
height()

height()

Get the current computed, pixel, height of the first matched element.

Returns

String

Example

jQuery Code

$("p").height();

Before

<p>This is just a test.</p>

Result:

300
height(val)

height(val)

Set the CSS height of every matched element. If no explicit unit was specified (like 'em' or '%') then "px" is added to the width.

Returns

jQuery

Parameters

  • val (String|Number): Set the CSS property to the specified value.

Example

jQuery Code

$("p").height(20);

Before

<p>This is just a test.</p>

Result:

<p style="height:20px;">This is just a test.</p>

Example

jQuery Code

$("p").height("20em");

Before

<p>This is just a test.</p>

Result:

<p style="height:20em;">This is just a test.</p>
width()

width()

Get the current computed, pixel, width of the first matched element.

Returns

String

Example

jQuery Code

$("p").width();

Before

<p>This is just a test.</p>

Result:

300
width(val)

width(val)

Set the CSS width of every matched element. If no explicit unit was specified (like 'em' or '%') then "px" is added to the width.

Returns

jQuery

Parameters

  • val (String|Number): Set the CSS property to the specified value.

Example

jQuery Code

$("p").width(20);

Before

<p>This is just a test.</p>

Result:

<p style="width:20px;">This is just a test.</p>

Example

jQuery Code

$("p").width("20em");

Before

<p>This is just a test.</p>

Result:

<p style="width:20em;">This is just a test.</p>
JavaScript
$.browser

$.browser

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.

There are situations where object detections is not reliable enough, in that cases it makes sense to use browser detection. Simply try to avoid both!

A combination of browser and object detection yields quite reliable results.

Returns

Boolean

Example

Returns true if the current useragent is some version of microsoft's internet explorer

jQuery Code

$.browser.msie

Example

Alerts "this is safari!" only for safari browsers

jQuery Code

if($.browser.safari) { $( function() { alert("this is safari!"); } ); }
$.each(obj, fn)

$.each(obj, fn)

A generic iterator function, which can be used to seamlessly 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.

The callback has two arguments:the key (objects) or index (arrays) as first the first, and the value as the second.

Returns

Object

Parameters

  • obj (Object): The object, or array, to iterate over.
  • fn (Function): The function that will be executed on every object.

Example

This is an example of iterating over the items in an array, accessing both the current item and its index.

jQuery Code

$.each( [0,1,2], function(i, n){
  alert( "Item #" + i + ": " + n );
});

Example

This is an example of iterating over the properties in an Object, accessing both the current item and its key.

jQuery Code

$.each( { name: "John", lang: "JS" }, function(i, n){
  alert( "Name: " + i + ", Value: " + n );
});
$.extend(target, prop1, propN)

$.extend(target, prop1, propN)

Extend one object with one or more others, returning the original, modified, object. This is a great utility for simple inheritance.

Returns

Object

Parameters

  • target (Object): The object to extend
  • prop1 (Object): The object that will be merged into the first.
  • propN (Object): (optional) More objects to merge into the first

Example

Merge settings and options, modifying settings

jQuery Code

var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
jQuery.extend(settings, options);

Result:

settings == { validate: true, limit: 5, name: "bar" }

Example

Merge defaults and options, without modifying the defaults

jQuery Code

var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
var settings = jQuery.extend({}, defaults, options);

Result:

settings == { validate: true, limit: 5, name: "bar" }
$.grep(array, fn, inv)

$.grep(array, fn, inv)

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 must return 'true' to keep the item in the array, false to remove it.

Returns

Array

Parameters

  • array (Array): The Array to find items in.
  • fn (Function): The function to process each item against.
  • inv (Boolean): Invert the selection - select the opposite of the function.

Example

jQuery Code

$.grep( [0,1,2], function(i){
  return i > 0;
});

Result:

[1, 2]
$.map(array, fn)

$.map(array, fn)

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.

Returns

Array

Parameters

  • array (Array): The Array to translate.
  • fn (Function): The function to process each item against.

Example

Maps the original array to a new one and adds 4 to each value.

jQuery Code

$.map( [0,1,2], function(i){
  return i + 4;
});

Result:

[4, 5, 6]

Example

Maps the original array to a new one and adds 1 to each value if it is bigger then zero, otherwise it's removed-

jQuery Code

$.map( [0,1,2], function(i){
  return i > 0 ? i + 1 : null;
});

Result:

[2, 3]

Example

Maps the original array to a new one, each element is added with it's original value and the value plus one.

jQuery Code

$.map( [0,1,2], function(i){
  return [ i, i + 1 ];
});

Result:

[0, 1, 1, 2, 2, 3]
$.merge(first, second)

$.merge(first, second)

Merge two arrays together, removing all duplicates.

The result is the altered first argument with the unique elements from the second array added.

Returns

Array

Parameters

  • first (Array): The first array to merge, the unique elements of second added.
  • second (Array): The second array to merge into the first, unaltered.

Example

Merges two arrays, removing the duplicate 2

jQuery Code

$.merge( [0,1,2], [2,3,4] )

Result:

[0,1,2,3,4]

Example

Merges two arrays, removing the duplicates 3 and 2

jQuery Code

var array = [3,2,1];
$.merge( array, [4,3,2] )

Result:

array == [3,2,1,4]
$.trim(str)

$.trim(str)

Remove the whitespace from the beginning and end of a string.

Returns

String

Parameters

  • str (String): The string to trim.

Example

jQuery Code

$.trim("  hello, how are you?  ");

Result:

"hello, how are you?"
Events
bind(type, data, fn)

bind(type, data, fn)

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.

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.

Returns

jQuery

Parameters

  • type (String): An event type
  • data (Object): (optional) Additional data passed to the event handler as event.data
  • fn (Function): A function to bind to the event on each of the set of matched elements

Example

jQuery Code

$("p").bind("click", function(){
  alert( $(this).text() );
});

Before

<p>Hello</p>

Result:

alert("Hello")

Example

Pass some additional data to the event handler.

jQuery Code

function handler(event) {
  alert(event.data.foo);
}
$("p").bind("click", {foo: "bar"}, handler)

Result:

alert("bar")

Example

Cancel a default action and prevent it from bubbling by returning false from your function.

jQuery Code

$("form").bind("submit", function() { return false; })

Example

Cancel only the default action by using the preventDefault method.

jQuery Code

$("form").bind("submit", function(event){
  event.preventDefault();
});

Example

Stop only an event from bubbling by using the stopPropagation method.

jQuery Code

$("form").bind("submit", function(event){
  event.stopPropagation();
});
blur()

blur()

Trigger the blur event of each matched element. 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.

Note: This does not execute the blur method of the underlying elements! If you need to blur an element via code, you have to use the DOM method, eg. $("#myinput")[0].blur();

Returns

jQuery

Example

jQuery Code

$("p").blur();

Before

<p onblur="alert('Hello');">Hello</p>

Result:

alert('Hello');
blur(fn)

blur(fn)

Bind a function to the blur event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the blur event on each of the matched elements.

Example

jQuery Code

$("p").blur( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onblur="alert('Hello');">Hello</p>
change(fn)

change(fn)

Bind a function to the change event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the change event on each of the matched elements.

Example

jQuery Code

$("p").change( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onchange="alert('Hello');">Hello</p>
click()

click()

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.

Returns

jQuery

Example

jQuery Code

$("p").click();

Before

<p onclick="alert('Hello');">Hello</p>

Result:

alert('Hello');
click(fn)

click(fn)

Bind a function to the click event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the click event on each of the matched elements.

Example

jQuery Code

$("p").click( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onclick="alert('Hello');">Hello</p>
dblclick(fn)

dblclick(fn)

Bind a function to the dblclick event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the dblclick event on each of the matched elements.

Example

jQuery Code

$("p").dblclick( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p ondblclick="alert('Hello');">Hello</p>
error(fn)

error(fn)

Bind a function to the error event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the error event on each of the matched elements.

Example

jQuery Code

$("p").error( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onerror="alert('Hello');">Hello</p>
focus()

focus()

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.

Note: This does not execute the focus method of the underlying elements! If you need to focus an element via code, you have to use the DOM method, eg. $("#myinput")[0].focus();

Returns

jQuery

Example

jQuery Code

$("p").focus();

Before

<p onfocus="alert('Hello');">Hello</p>

Result:

alert('Hello');
focus(fn)

focus(fn)

Bind a function to the focus event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the focus event on each of the matched elements.

Example

jQuery Code

$("p").focus( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onfocus="alert('Hello');">Hello</p>
hover(over, out)

hover(over, out)

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).

Returns

jQuery

Parameters

  • over (Function): The function to fire whenever the mouse is moved over a matched element.
  • out (Function): The function to fire whenever the mouse is moved off of a matched element.

Example

jQuery Code

$("p").hover(function(){
  $(this).addClass("hover");
},function(){
  $(this).removeClass("hover");
});
keydown(fn)

keydown(fn)

Bind a function to the keydown event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the keydown event on each of the matched elements.

Example

jQuery Code

$("p").keydown( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onkeydown="alert('Hello');">Hello</p>
keypress(fn)

keypress(fn)

Bind a function to the keypress event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the keypress event on each of the matched elements.

Example

jQuery Code

$("p").keypress( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onkeypress="alert('Hello');">Hello</p>
keyup(fn)

keyup(fn)

Bind a function to the keyup event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the keyup event on each of the matched elements.

Example

jQuery Code

$("p").keyup( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onkeyup="alert('Hello');">Hello</p>
load(fn)

load(fn)

Bind a function to the load event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the load event on each of the matched elements.

Example

jQuery Code

$("p").load( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onload="alert('Hello');">Hello</p>
mousedown(fn)

mousedown(fn)

Bind a function to the mousedown event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the mousedown event on each of the matched elements.

Example

jQuery Code

$("p").mousedown( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onmousedown="alert('Hello');">Hello</p>
mousemove(fn)

mousemove(fn)

Bind a function to the mousemove event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the mousemove event on each of the matched elements.

Example

jQuery Code

$("p").mousemove( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onmousemove="alert('Hello');">Hello</p>
mouseout(fn)

mouseout(fn)

Bind a function to the mouseout event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the mouseout event on each of the matched elements.

Example

jQuery Code

$("p").mouseout( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onmouseout="alert('Hello');">Hello</p>
mouseover(fn)

mouseover(fn)

Bind a function to the mouseover event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the mousedown event on each of the matched elements.

Example

jQuery Code

$("p").mouseover( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onmouseover="alert('Hello');">Hello</p>
mouseup(fn)

mouseup(fn)

Bind a function to the mouseup event of each matched element.

Returns

jQuery

Parameters

  • fn (Function): A function to bind to the mouseup event on each of the matched elements.

Example

jQuery Code

$("p").mouseup( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onmouseup="alert('Hello');">Hello</p>
one(type, data, fn)

one(type, data, fn)

Binds a handler to a particular event (like click) for each matched element. The handler is executed only once for each element. Otherwise, the same rules as described in bind() 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 has to return false.

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.

Returns

jQuery

Parameters

  • type (String): An event type
  • data (Object): (optional) Additional data passed to the event handler as event.data
  • fn (Function): A function to bind to the event on each of the set of matched elements

Example

jQuery Code

$("p").one("click", function(){
  alert( $(this).text() );
});

Before

<p>Hello</p>

Result:

alert("Hello")
ready(fn)

ready(fn)