2

I asked a question about sending data from a class to it's parent class (I know js has no classes but I've used prototype).

Sending data from classes?

It was suggested to use delegate.

I read some more about delegate and find in the jquery docs it's out of date. So what should I be using? on? If so how would it work in my example? All the examples for .on refer to buttons or dom elements.

  • "and find in the jquery docs it's out of date" --- can you clarify what you mean? – zerkms Jun 05 '14 at 11:08
  • 1
    the term to describe out-of-dateness in the programming world is `deprecated`. – MarioDS Jun 05 '14 at 11:08
  • Note that the answer you're linking to talks about delegating in general, not about jQuery's `.delegate()`. Delegating *as a concept* itself isn't "out of date". – JJJ Jun 05 '14 at 11:10
  • @MDeSchaepmeester: your change is not correct - it was **NOT** deprecated. At least jquery dev team didn't state they deprecate it. – zerkms Jun 05 '14 at 11:11
  • So is what was suggested correct? – user3710433 Jun 05 '14 at 11:11
  • `delegate` method isn't deprecated, it has been superseded by the .on() method, not the same thing – A. Wolff Jun 05 '14 at 11:12
  • @zerkms Oh I never saw that. I knew it was an older method though so I assumed it was. Sorry. – MarioDS Jun 05 '14 at 11:13
  • Can someone please just answer my questions and stop going out about whether it was depreciated or not – user3710433 Jun 05 '14 at 11:13
  • @user3710433 You have already your answer... Use preferably on() – A. Wolff Jun 05 '14 at 11:14
  • @A.Wolff My question asked for an example of this – user3710433 Jun 05 '14 at 11:15
  • 1
    @user3710433 We aren't paid to provide you copy/paste sample of whatever you want, make some research, read the DOC, take some initiative – A. Wolff Jun 05 '14 at 11:16
  • The OP's question has nothing to do with `.delegate()`. It's just a misunderstanding based on similar variable names in the other question. As such it's not a duplicate. – JJJ Jun 05 '14 at 11:18
  • @Juhana So what is the question??? Are you referring to older question posted as link here? – A. Wolff Jun 05 '14 at 11:20
  • @A.Wolff Yes. *"I asked a question [..] It was suggested to use delegate."* But the other question's answer *didn't* suggest to use `.delegate()` -- the OP only thought so and got confused by the API saying it's deprecated. The answer to the question is "`.delegate()` is deprecated, but you weren't told to use it, so no problems". The answer "use `.on()` instead" is of no use to the OP. – JJJ Jun 05 '14 at 11:23
  • @Juhana Ok i see what you mean after reading OP's older question – A. Wolff Jun 05 '14 at 11:27
  • @Juhana quite right, post it as an answer an i'll accept it – user3710433 Jun 05 '14 at 11:28
  • I can't post an answer because the question is closed. – JJJ Jun 05 '14 at 11:28

1 Answers1

4

From the jQuery Doc:

As of jQuery 1.7, .delegate() has been superseded by the .on() method. For earlier versions, however, it remains the most effective means to use event delegation.

So, it's not out of date, it's just that another method is preferred in newer versions. AFAIK it is still existing for backwards compatibility.

In fact, the delegate() method in newer jQuery versions calls the on() method but switches up the parameters.

Delegate in jQuery 1.7+

function (selector, types, data, fn) {
    return this.live(types, data, fn, selector);
}

TL;DR: For jQuery versions prior to 1.7, use delegate(). For newer versions, use on().

George
  • 36,413
  • 9
  • 66
  • 103
  • 1
    This does not answer my question. – user3710433 Jun 05 '14 at 11:13
  • @user3710433: it does answer your only "What is the alternative to JQuery`s deprecated delegate()?" question. – zerkms Jun 05 '14 at 11:14
  • @user3710433 Looks like hard time for everyone to get your question – A. Wolff Jun 05 '14 at 11:15
  • 1
    @user3710433 TL;DR: For jQuery versions prior to 1.7, use delegate(). For newer versions, use on(). – George Jun 05 '14 at 11:15
  • Guys, thats what has been on my mind also. I made a fiddle illustrating problem im having with this issue. clicking on div will make event fire twice, even if the container does not have ".b" class anymore http://jsfiddle.net/TEP3X/ and im not interested in using "one()". I just wanna dynamic event, dependent on node having class – entio Jun 05 '14 at 11:16
  • 1
    @entio How is that related? Ask your own question. – George Jun 05 '14 at 11:18
  • @oGeez; Jeez i was trying to search it and am afraid of stack nazis... couldnt You yust tell my what to seek for? – entio Jun 05 '14 at 11:19
  • 1
    @entio Stack Nazis? Write a good question, with evidence of your own research, what you have and what you desire. If it's a good enough question it'll stay open. You can't expect people to guess what you want. – George Jun 05 '14 at 11:21
  • 2
    @user3710433 You should be more polite with people trying to help you... Now i guess, many will stop trying to help you, i hope – A. Wolff Jun 05 '14 at 11:21
  • Sorry for being dumb. Jquery documentation is sooo clearly about this. – entio Jun 05 '14 at 11:28
  • 1
    @entio `$(document.body).on('click','div.a.b',func...` http://api.jquery.com/on/ It means you listen to the container of element(s) you're interested in. when you change the html the click will still fire on the container. Behind the scenes jQuery will check if the clicked element satisfies the second selector and if so it'll fire your listener. `$('selector').on('click',functi...` will add the event listener to that element and changing the classes on that element won't undo that. – HMR Jun 05 '14 at 14:37