Is jquery .bind() deprecated?

14,977

Solution 1

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.

Solution 2

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.

Solution 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

Solution 4

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

Share:
14,977
Claudiu Creanga
Author by

Claudiu Creanga

pancakes

Updated on July 22, 2022

Comments

  • Claudiu Creanga
    Claudiu Creanga almost 2 years

    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/

  • Claudiu Creanga
    Claudiu Creanga over 8 years
    can you also listen to dom changes with on?
  • T.J. Crowder
    T.J. Crowder over 8 years
    @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. MutationObserver is much better supported than the old events ever were.
  • AtheistP3ace
    AtheistP3ace over 8 years
    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.
  • Kevin B
    Kevin B over 8 years
    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)
  • AtheistP3ace
    AtheistP3ace over 8 years
    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.
  • Kevin B
    Kevin B over 8 years
    That isn't true, for .on to support delegated events, the $("") part still has to select an existing element, it's no different.
  • AtheistP3ace
    AtheistP3ace over 8 years
    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.
  • T.J. Crowder
    T.J. Crowder over 8 years
    @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
    T.J. Crowder over 8 years
    @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.)
  • AtheistP3ace
    AtheistP3ace over 8 years
    Yes, @T.J.Crowder that is what I am trying to say. I apologize to all if that wasn't clear.
  • Neo
    Neo over 6 years
    Man there are a ton of libraries that STILL use bind
  • Denis G. Labrecque
    Denis G. Labrecque almost 4 years
    This is definitely deprecated as of version 3 of jQuery. Use jQuery migrate.