Skip to content

Commit 0ff4f80

Browse files
jouvekswedberg
authored andcommitted
replace uses of .bind/.unbind with prefered .on/.off
1 parent 547334b commit 0ff4f80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+56
-56
lines changed

entries/blur.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<added>1.0</added>
2222
</signature>
2323
<longdesc>
24-
<p>This method is a shortcut for <code>.bind('blur', handler)</code> in the first two variations, and <code>.trigger('blur')</code> in the third.</p>
24+
<p>This method is a shortcut for <code>.on('blur', handler)</code> in the first two variations, and <code>.trigger('blur')</code> in the third.</p>
2525
<p>The <code>blur</code> event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as <code>&lt;input&gt;</code>. In recent browsers, the domain of the event has been extended to include all element types. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page.</p>
2626
<p>For example, consider the HTML:</p>
2727
<pre><code>&lt;form&gt;

entries/change.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<added>1.0</added>
2222
</signature>
2323
<longdesc>
24-
<p>This method is a shortcut for <code>.bind('change', handler)</code> in the first two variations, and <code>.trigger('change')</code> in the third.</p>
24+
<p>This method is a shortcut for <code>.on('change', handler)</code> in the first two variations, and <code>.trigger('change')</code> in the third.</p>
2525
<p>The <code>change</code> event is sent to an element when its value changes. This event is limited to <code>&lt;input&gt;</code> elements, <code>&lt;textarea&gt;</code> boxes and <code>&lt;select&gt;</code> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.</p>
2626
<p>For example, consider the HTML:</p>
2727
<pre><code>&lt;form&gt;

entries/click.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
<added>1.0</added>
2222
</signature>
2323
<longdesc>
24-
<p>In the first two variations, this method is a shortcut for <code>.bind("click", handler)</code>, as well as for <code>.on("click", handler)</code> as of jQuery 1.7. In the third variation, when <code>.click()</code> is called without arguments, it is a shortcut for <code>.trigger("click")</code>. </p>
25-
<p>The <code>click</code> event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed and released. Any HTML element can receive this event.</p>
26-
<pre><code>For example, consider the HTML:
27-
&lt;div id="target"&gt;
24+
<p>This method is a shortcut for <code>.on('click', handler)</code> in the first two variations, and <code>.trigger('click')</code> in the third.
25+
The <code>click</code> event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed and released. Any HTML element can receive this event.
26+
For example, consider the HTML:</p>
27+
<pre><code>&lt;div id="target"&gt;
2828
Click here
2929
&lt;/div&gt;
3030
&lt;div id="other"&gt;

entries/closest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ $('li.item-a').closest('#one', listItemII)
101101
<example>
102102
<desc>Show how event delegation can be done with closest. The closest list element toggles a yellow background when it or its descendent is clicked.</desc>
103103
<code><![CDATA[
104-
$( document ).bind("click", function( e ) {
104+
$( document ).on("click", function( e ) {
105105
$( e.target ).closest("li").toggleClass("hilight");
106106
});
107107
]]></code>
@@ -118,7 +118,7 @@ $('li.item-a').closest('#one', listItemII)
118118
<desc>Pass a jQuery object to closest. The closest list element toggles a yellow background when it or its descendent is clicked.</desc>
119119
<code><![CDATA[
120120
var $listElements = $("li").css("color", "blue");
121-
$( document ).bind("click", function( e ) {
121+
$( document ).on("click", function( e ) {
122122
$( e.target ).closest( $listElements ).toggleClass("hilight");
123123
});
124124
]]></code>

entries/dblclick.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<added>1.0</added>
2222
</signature>
2323
<longdesc>
24-
<p>This method is a shortcut for <code>.bind('dblclick', handler)</code> in the first two variations, and <code>.trigger('dblclick')</code> in the third.
24+
<p>This method is a shortcut for <code>.on('dblclick', handler)</code> in the first two variations, and <code>.trigger('dblclick')</code> in the third.
2525
The <code>dblclick</code> event is sent to an element when the element is double-clicked. Any HTML element can receive this event.
2626
For example, consider the HTML:</p>
2727
<pre><code>&lt;div id="target"&gt;

entries/deferred.done.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dfd
5353
});
5454
5555
/* resolve the Deferred object when the button is clicked */
56-
$("button").bind("click", function() {
56+
$("button").on("click", function() {
5757
dfd.resolve("and");
5858
});
5959
]]></code>

entries/die.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
</argument>
2222
</signature>
2323
<longdesc>
24-
<p>Any handler that has been attached with <code>.live()</code> can be removed with <code>.die()</code>. This method is analogous to calling <code>.unbind()</code> with no arguments, which is used to remove all handlers attached with <code>.bind()</code>.
25-
See the discussions of <code>.live()</code> and <code>.unbind()</code> for further details.</p>
24+
<p>Any handler that has been attached with <code>.live()</code> can be removed with <code>.die()</code>. This method is analogous to calling <code>.off()</code> with no arguments, which is used to remove all handlers attached with <code>.on()</code>.
25+
See the discussions of <code>.live()</code> and <code>.off()</code> for further details.</p>
2626
<p>If used without an argument, .die() removes <em>all</em> event handlers previously attached using <code>.live()</code> from the elements.</p>
2727
<p><strong>As of jQuery 1.7</strong>, use of <code>.die()</code> (and its complementary method, <code>.live()</code>) is not recommended. Instead, use <a href="http://api.jquery.com/off/"><code>.off()</code></a> to remove event handlers bound with <a href="http://api.jquery.com/on/"><code>.on()</code></a></p>
2828
<p><strong>Note:</strong> In order for .die() to function correctly, the selector used with it must match exactly the selector initially used with .live().</p>

entries/error.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</argument>
1919
</signature>
2020
<longdesc>
21-
<p>This method is a shortcut for <code>.bind('error', handler)</code>.</p>
21+
<p>This method is a shortcut for <code>.on('error', handler)</code>.</p>
2222
<p>The <code>error</code> event is sent to elements, such as images, that are referenced by a document and loaded by the browser. It is called if the element was not loaded correctly.</p>
2323
<p>For example, consider a page with a simple image element:</p>
2424
<pre><code>&lt;img alt="Book" id="book" /&gt;</code></pre>

entries/event.namespace.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<example>
1212
<desc>Determine the event namespace used.</desc>
1313
<code><![CDATA[
14-
$("p").bind("test.something", function(event) {
14+
$("p").on("test.something", function(event) {
1515
alert( event.namespace );
1616
});
1717
$("button").click(function(event) {

entries/event.pageX.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<css>body {background-color: #eef; }
1212
div { padding: 20px; }</css>
1313
<html><![CDATA[<div id="log"></div>]]></html>
14-
<code><![CDATA[$(document).bind('mousemove',function(e){
14+
<code><![CDATA[$(document).on('mousemove',function(e){
1515
$("#log").text("e.pageX: " + e.pageX + ", e.pageY: " + e.pageY);
1616
}); ]]></code>
1717
</example>

entries/event.pageY.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</css>
1414
<html><![CDATA[<div id="log"></div>]]></html>
1515
<code><![CDATA[
16-
$(document).bind('mousemove',function(e){
16+
$(document).on('mousemove',function(e){
1717
$("#log").text("e.pageX: " + e.pageX + ", e.pageY: " + e.pageY);
1818
});
1919
]]></code>

entries/event.which.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</longdesc>
1212
<example>
1313
<desc>Log which key was depressed.</desc>
14-
<code><![CDATA[$('#whichkey').bind('keydown',function(e){
14+
<code><![CDATA[$('#whichkey').on('keydown',function(e){
1515
$('#log').html(e.type + ': ' + e.which );
1616
}); ]]></code>
1717
<html><![CDATA[
@@ -21,7 +21,7 @@
2121
<example>
2222
<desc>Log which mouse button was depressed.</desc>
2323
<code><![CDATA[
24-
$('#whichkey').bind('mousedown',function(e){
24+
$('#whichkey').on('mousedown',function(e){
2525
$('#log').html(e.type + ': ' + e.which );
2626
});
2727
]]></code>

entries/focus.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</signature>
2323
<longdesc>
2424
<ul>
25-
<li>This method is a shortcut for <code>.bind('focus', handler)</code> in the first and second variations, and <code>.trigger('focus')</code> in the third.</li>
25+
<li>This method is a shortcut for <code>.on('focus', handler)</code> in the first and second variations, and <code>.trigger('focus')</code> in the third.</li>
2626
<li>The <code>focus</code> event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<code>&lt;input&gt;</code>, <code>&lt;select&gt;</code>, etc.) and links (<code>&lt;a href&gt;</code>). In recent browser versions, the event can be extended to include all element types by explicitly setting the element's <code>tabindex</code> property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.</li>
2727
<li>Elements with focus are usually highlighted in some way by the browser, for example with a dotted line surrounding the element. The focus is used to determine which element is the first to receive keyboard-related events.</li>
2828
</ul>

entries/focusin.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</argument>
1919
</signature>
2020
<longdesc>
21-
<p>This method is a shortcut for <code>.bind('focusin', handler)</code>.</p>
21+
<p>This method is a shortcut for <code>.on('focusin', handler)</code>.</p>
2222
<p>The <code>focusin</code> event is sent to an element when it, or any element inside of it, gains focus. This is distinct from the <a href="/focus">focus</a> event in that it supports detecting the focus event on parent elements (in other words, it supports event bubbling).</p>
2323
<p>This event will likely be used together with the <a href="/focusout">focusout</a> event.</p>
2424
</longdesc>

entries/focusout.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</argument>
1919
</signature>
2020
<longdesc>
21-
<p>This method is a shortcut for <code>.bind('focusout', handler)</code>.</p>
21+
<p>This method is a shortcut for <code>.on('focusout', handler)</code>.</p>
2222
<p>The <code>focusout</code> event is sent to an element when it, or any element inside of it, loses focus. This is distinct from the <a href="/blur">blur</a> event in that it supports detecting the loss of focus on descendant elements (in other words, it supports event bubbling).</p>
2323
<p>This event will likely be used together with the <a href="/focusin">focusin</a> event.</p>
2424
</longdesc>

entries/hover.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ $("li.fade").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
6161
</example>
6262
<example>
6363
<desc>To unbind the above example use:</desc>
64-
<code><![CDATA[$("td").unbind('mouseenter mouseleave');]]></code>
64+
<code><![CDATA[$("td").off('mouseenter mouseleave');]]></code>
6565
</example>
6666
<category slug="events/mouse-events"/>
6767
<category slug="version/1.0"/>
@@ -77,7 +77,7 @@ $("li.fade").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});
7777
<longdesc>
7878
<p>The <code>.hover()</code> method, when passed a single function, will execute that handler for both <code>mouseenter</code> and <code>mouseleave</code> events. This allows the user to use jQuery's various toggle methods within the handler or to respond differently within the handler depending on the <code>event.type</code>.</p>
7979
<p>Calling <code>$(selector).hover(handlerInOut)</code> is shorthand for:</p>
80-
<pre><code>$(selector).bind("mouseenter mouseleave", handlerInOut);</code></pre>
80+
<pre><code>$(selector).on("mouseenter mouseleave", handlerInOut);</code></pre>
8181
<p>See the discussions for <code><a href="/mouseenter">.mouseenter()</a></code> and <code><a href="/mouseleave">.mouseleave()</a></code> for more details.</p>
8282
</longdesc>
8383
<example>

entries/jQuery.proxy.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var me = {
6969
/* With proxy, `this` refers to the me object encapsulating */
7070
/* this function. */
7171
$("#log").append( "Hello " + this.type + "<br>" );
72-
$("#test").unbind("click", this.test);
72+
$("#test").off("click", this.test);
7373
}
7474
};
7575

entries/jQuery.sub.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typeof jQuery('body').myCustomMethod // undefined]]></code>
5050
});
5151
5252
// A new remove event is now triggered from this copy of jQuery
53-
$(document).bind("remove", function(e) {
53+
$(document).on("remove", function(e) {
5454
$(e.target).parent().hide();
5555
});
5656
});

entries/jQuery.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ $.post( "url.xml", function(data) {
7979
<h4 id="returning-empty-set">Returning an Empty Set</h4>
8080
<p>As of jQuery 1.4, calling the <code>jQuery()</code> method with <em>no arguments</em> returns an empty jQuery set (with a <code><a href="http://api.jquery.com/length/">.length</a></code> property of 0). In previous versions of jQuery, this would return a set containing the document node.</p>
8181
<h4 id="working-with-plain-objects">Working With Plain Objects</h4>
82-
<p>At present, the only operations supported on plain JavaScript objects wrapped in jQuery are: <code>.data()</code>,<code>.prop()</code>,<code>.bind()</code>, <code>.unbind()</code>, <code>.trigger()</code> and <code>.triggerHandler()</code>. The use of <code>.data()</code> (or any method requiring <code>.data()</code>) on a plain object will result in a new property on the object called jQuery{randomNumber} (eg. jQuery123456789).</p>
82+
<p>At present, the only operations supported on plain JavaScript objects wrapped in jQuery are: <code>.data()</code>,<code>.prop()</code>,<code>.on()</code>, <code>.off()</code>, <code>.trigger()</code> and <code>.triggerHandler()</code>. The use of <code>.data()</code> (or any method requiring <code>.data()</code>) on a plain object will result in a new property on the object called jQuery{randomNumber} (eg. jQuery123456789).</p>
8383
<pre><code>
8484
// define a plain object
8585
var foo = {foo: "bar", hello: "world"};
@@ -99,7 +99,7 @@ $foo.data( "keyName", "someValue");
9999
console.log( $foo ); // will now contain a jQuery{randomNumber} property
100100

101101
// test binding an event name and triggering
102-
$foo.bind( "eventName", function () {
102+
$foo.on( "eventName", function () {
103103
console.log("eventName was called");
104104
});
105105

entries/keydown.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</signature>
2222
<desc>Bind an event handler to the "keydown" JavaScript event, or trigger that event on an element.</desc>
2323
<longdesc>
24-
<p>This method is a shortcut for <code>.bind('keydown', handler)</code> in the first and second variations, and <code>.trigger('keydown')</code> in the third.</p>
24+
<p>This method is a shortcut for <code>.on('keydown', handler)</code> in the first and second variations, and <code>.trigger('keydown')</code> in the third.</p>
2525
<p>The <code>keydown</code> event is sent to an element when the user first presses a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.</p>
2626
<p>For example, consider the HTML:</p>
2727
<pre><code>&lt;form&gt;

entries/keypress.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</signature>
2323
<longdesc>
2424
<p><strong>Note:</strong> as the <code>keypress</code> event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.</p>
25-
<p>This method is a shortcut for <code>.bind("keypress", handler)</code> in the first two variations, and <code>.trigger("keypress")</code> in the third.</p>
25+
<p>This method is a shortcut for <code>.on('keypress', handler)</code> in the first two variations, and <code>.trigger('keypress')</code> in the third.</p>
2626
<p>The <code>keypress</code> event is sent to an element when the browser registers keyboard input. This is similar to the <code>keydown</code> event, except in the case of key repeats. If the user presses and holds a key, a <code>keydown </code>event is triggered once, but separate <code>keypress</code> events are triggered for each inserted character. In addition, modifier keys (such as Shift) trigger <code>keydown</code> events but not <code>keypress</code> events.</p>
2727
<p>A <code>keypress</code> event handler can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.</p>
2828
<p>For example, consider the HTML:</p>

entries/keyup.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<added>1.0</added>
2222
</signature>
2323
<longdesc>
24-
<p>This method is a shortcut for <code>.bind('keyup', handler)</code> in the first two variations, and <code>.trigger('keyup')</code> in the third.</p>
24+
<p>This method is a shortcut for <code>.on('keyup', handler)</code> in the first two variations, and <code>.trigger('keyup')</code> in the third.</p>
2525
<p>The <code>keyup</code> event is sent to an element when the user releases a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.</p>
2626
<p>For example, consider the HTML:</p>
2727
<pre><code>&lt;form&gt;

entries/live.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $(document).on("click", "a.offsite", function(){ alert("Goodbye!"); }); /
5353
<ol><li>Use natively clickable elements such as <code>a</code> or <code>button</code>, as both of these do bubble to <code>document</code>.</li><li>Use <code>.on()</code> or <code>.delegate()</code> attached to an element below the level of <code>document.body</code>, since mobile iOS does bubble within the body.</li><li>Apply the CSS style <code>cursor:pointer</code> to the element that needs to bubble clicks (or a parent including <code>document.documentElement</code>). Note however, this will disable copy\paste on the element and cause it to be highlighted when touched.</li></ol>
5454
</li>
5555
<li>Calling <a href="http://api.jquery.com/event.stopPropagation"><code>event.stopPropagation()</code></a> in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated to <code>document</code>.</li>
56-
<li>The <code>.live()</code> method interacts with other event methods in ways that can be surprising, e.g., <code>$(document).unbind("click")</code> removes all click handlers attached by any call to <code>.live()</code>!</li>
56+
<li>The <code>.live()</code> method interacts with other event methods in ways that can be surprising, e.g., <code>$(document).off("click")</code> removes all click handlers attached by any call to <code>.live()</code>!</li>
5757
</ul>
5858
<p>For pages still using <code>.live()</code>, this list of version-specific differences may be helpful:</p>
5959
<ul>

entries/load-event.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</argument>
1919
</signature>
2020
<longdesc>
21-
<p>This method is a shortcut for <code>.bind('load', handler)</code>.</p>
21+
<p>This method is a shortcut for <code>.on('load', handler)</code>.</p>
2222
<p>The <code>load</code> event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the <code>window</code> object.</p>
2323
<p>For example, consider a page with a simple image:</p>
2424
<pre><code>&lt;img src="book.png" alt="Book" id="book" /&gt;</code></pre>

entries/mousedown.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<added>1.0</added>
2222
</signature>
2323
<longdesc>
24-
<p>This method is a shortcut for <code>.bind('mousedown', handler)</code> in the first variation, and <code>.trigger('mousedown')</code> in the second.</p>
24+
<p>This method is a shortcut for <code>.on('mousedown', handler)</code> in the first variation, and <code>.trigger('mousedown')</code> in the second.</p>
2525
<p>The <code>mousedown</code> event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed. Any HTML element can receive this event.</p>
2626
<p>For example, consider the HTML:</p>
2727
<pre><code>&lt;div id="target"&gt;

0 commit comments

Comments
 (0)