1

I have just downloaded jQuery 1.7 for a brand new project I'm starting.

After reading the the docs I see .on() now replaces .live(). Quote from the docs:

As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For equivalents to older jQuery event methods, see .bind(), .delegate(), and .live().

So what I'm trying to do is quite simple. I have the following jQuery which of course uses the old method:

$('a').live('click', function(e){
    e.preventDefault(); 
});

This works just fine, by that I mean the anchors' default behaviour is prevented. If I use this:

$('a').on('click', function(e){
    e.preventDefault(); 
});

The anchors' default behaviour is not prevented and when clicked it loads another page. Am I doing something blatantly wrong here or have I misunderstood .on()?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
mtwallet
  • 5,040
  • 14
  • 50
  • 74
  • Jquery changes method names WAY too often.. – Rob Nov 04 '11 at 15:55
  • @Rob: however that might be true, the 'older' ones are still there. Also it makes it just easier by unifying all the different ways of attaching events which is a good thing. – PeeHaa Nov 04 '11 at 16:01

1 Answers1

2

It DOES work.

http://jsfiddle.net/RsHnn/

Are you sure you don't have any JS error on that page / you are sure you are using jQuery 1.7?

EDIT

It looks like you need to add the selector if you want it the work with dynamically added elements.

http://jsfiddle.net/RsHnn/2/

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • I am certain I have no errors which is weird. Thanks for the help though I do appreciate it! – mtwallet Nov 04 '11 at 16:10
  • @mtwallet: Can I see the page somewhere (can you provide a link) so I can see what happens. Or do you only have it internal? Also what browser or you on? – PeeHaa Nov 04 '11 at 16:11
  • sorry its local but I can tell you I've tested Chrome, Firefox and Safari on Mac OSX – mtwallet Nov 04 '11 at 16:13
  • 1
    However the anchor I am trying to click is ajaxed. – mtwallet Nov 04 '11 at 16:15
  • So I see. Again many thanks for your help I'll pick apart my code and try to fathom it out, it must be something I've screwed up! – mtwallet Nov 04 '11 at 16:26