10

Is jquery bind() deprecated or it can be used safely?

I see a lot of comments about bind() being deprecated in comments and answers across SO like: Jquery Event : Detect changes to the html/text of a div

Is there a JavaScript/jQuery DOM change listener?

and don't know if it is safe to use it or not (regardless of it being better or worse).

There is nothing about it being deprecated here: https://api.jquery.com/bind/

Community
  • 1
  • 1
Claudiu Creanga
  • 8,031
  • 10
  • 71
  • 110
  • 7
    From the API documentation you linked to: *“As of jQuery 1.7, the .on() method is the preferred method for attaching event handlers to a document.”* – Ry- Nov 11 '15 at 16:08
  • 1
    meaning, no it isn't deprecated, and it is safe to use. just not... *"preferred"* – Kevin B Nov 11 '15 at 16:08
  • 4
    .bind is typically used by plugins that are backwards compatible to much older versions of jQuery, which is why it won't be going away any time soon. – Kevin B Nov 11 '15 at 16:09
  • 1
    I think in the later version of jQuery bind() just uses .on() under the hood anyway. So if you use the latest version there is very little difference between using bind and on apart from the syntax where you call it – Liam Nov 11 '15 at 16:09
  • This is definitely deprecated as of version 3 - http://api.jquery.com/bind/ – Rob Sep 25 '16 at 06:35

4 Answers4

14

I don't believe its deprecated but on is the preferred method of attaching events.

http://api.jquery.com/bind/

Deprecated means they will be removing it in the future but their docs don't seem to say that.

EDIT:

As of jQuery 3.0 bind IS deprecated. The above link is still relevant but has since been updated. From their docs:

As of jQuery 3.0, .bind() has been deprecated. It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged.

AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
  • can you also listen to dom changes with on? – Claudiu Creanga Nov 11 '15 at 16:12
  • 2
    @Claudiu: You can't listen to DOM changes with either `.on` **or** `.bind`. Mutation events were never well-supported, and have been ditched in favor of [mutation observers](https://developer.mozilla.org/en/docs/Web/API/MutationObserver). MutationObserver is much better supported than the old events ever were. – T.J. Crowder Nov 11 '15 at 16:14
  • What do you mean by DOM changes? Anything you did with bind you can do with on and more. Since I believe bind did not support delegated events. In other words, the element had to exist when the call to bind took place. – AtheistP3ace Nov 11 '15 at 16:14
  • 2
    Man there are a ton of libraries that STILL use bind – Neo Oct 09 '17 at 16:51
5

No it's not, that are a difference between .on and .bind basically .bind is only called directly from a element i.e. $("#elem").bind(...), so the element must exist by the time the bind function is called. And .on can be "bind" on document i. e. $(document).on("click",".class",funcion(){...});, so if you add an element dynamically by using .append or other way, the event will be valid.

Marcus Abrahão
  • 696
  • 1
  • 8
  • 18
  • can you expand a bit on that difference? I don't quite understand what the difference is even after reading your answer. (I don't think there is a difference, other than .bind having less functionality) – Kevin B Nov 11 '15 at 16:15
  • 1
    Bind does not support delegated events. The element has to exist in the DOM in order to attach the event. Where with on you can attach the event to parent object even when the element you want the event to happen on does not exist yet. – AtheistP3ace Nov 11 '15 at 16:16
  • That isn't true, for .on to support delegated events, the $("") part still has to select an existing element, it's no different. – Kevin B Nov 11 '15 at 16:17
  • Well of course, its delegated. Something has to exist, but it still was not possible to attach a delegated event with bind. You had to use delegate function. – AtheistP3ace Nov 11 '15 at 16:18
  • 2
    @KevinB: I think what Marcus means is: Yes, while the bit you look up has to exist, `on` can be used to catch events on elements that don't exist yet. Like `$(document).on("click", ".foo", ...)` to catch events on `class="foo"` elements that don't exist yet. (`document` being the broadest possible delegation root.) – T.J. Crowder Nov 11 '15 at 16:18
  • 2
    @AtheistP3ace: Right, `.bind` didn't provide direct delegation support (you could do it yourself, of course). Pre v1.7, it was separate: `.bind` for direct, `.delegate` for delgated. (And the horror that was `.live`.) – T.J. Crowder Nov 11 '15 at 16:21
  • Yes, @T.J.Crowder that is what I am trying to say. I apologize to all if that wasn't clear. – AtheistP3ace Nov 11 '15 at 16:22
3

It's not deprecated in jQuery 1.1. There is actually no difference (apart form it's usage) in version jquery 1.11.3 if you inspect the declaration of the .bind event you can see it simply calls .on:

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
},

deprecated or it can be used safely?

As others say, no it's not deprecated and is safe, it's just a bit redundant now and only exists for backwards compatibility.

Though it does seem to of been marked as deprecated in later versions of jquery now

Liam
  • 27,717
  • 28
  • 128
  • 190
3

.bind() and .delegate() are deprecated as of jQuery 1.12 and 2.2. These functions work well with no warning message in 1.12 or 2.2, but will not work in next version and (of course) in 3.0.

It is strongly recommended to use .on() instead of .bind() and .delegate().

http://blog.jquery.com/2016/01/08/jquery-2-2-and-1-12-released/ https://github.com/jquery/jquery/issues/2288

j08691
  • 204,283
  • 31
  • 260
  • 272
Jonghee Park
  • 1,257
  • 15
  • 14