-1

I have this code i used in an older version of jquery and we updated to a new version. Live is no longer supported how can i change this to using the On keyword thanks.

  ('form').live("submit", function (event) {
            mixpanel.track("Action - Sent Email Message");
        });
Corey Toolis
  • 307
  • 1
  • 3
  • 21

2 Answers2

3

Use on()

$('form').on('submit',function(event){

if the element is dynamic delegate on the closest static element like this

$(document).on('submit','form',function(event){

});
Anton
  • 32,245
  • 5
  • 44
  • 54
0

the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

you need to update the jquery versions as well in order to support for .on() jQuery 1.7+

Gourav
  • 1,765
  • 2
  • 15
  • 16
  • I think the OP knows that. Quoting them: *"Live is no longer supported how can i change this to using the On keyword"* – Felix Kling Sep 16 '13 at 15:05
  • by using $('element').on('submit',function(event){}); – Gourav Sep 16 '13 at 15:08
  • No, that's not the equivalent to the `.live` version. That would bind the event handler directly to `'element'`. You need to use the *event delegation* version of `.on`: http://api.jquery.com/on/#direct-and-delegated-events. – Felix Kling Sep 16 '13 at 15:12
  • @FelixKling : .on() is delegated events. read it carefully. – Gourav Sep 16 '13 at 15:13
  • I did. It says *"If `selector` is omitted or is null, the event handler is referred to as direct or directly-bound. [...] When a `selector` is provided, the event handler is referred to as delegated.*" You don't pass a selector to `.on`, hence your handler is not delegated. It seems like *you* need to read the documentation more carefully. Maybe it becomes clearer after you had a look at http://learn.jquery.com/events/event-delegation/. `.on` is used for both, "normal" event handling and event delegation. – Felix Kling Sep 16 '13 at 15:17
  • ok fine.But the delegated events are used to attach the event handlers. – Gourav Sep 16 '13 at 15:22
  • *"But the delegated events are used to attach the event handlers."* I don't know what you mean by that. In any case, the OP asked how to use `.on` instead of `.live` and your answer doesn't provide a solution for that. That's all I wanted to point out. – Felix Kling Sep 16 '13 at 15:33
  • Ok brother thanks for guiding me and making me understand the concept. – Gourav Sep 16 '13 at 15:36