Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.live() to .on() issue

Tags:

jquery

I have a problem that can not solve.

This code works perfect for me.

// click row opens message, unless you click delete    
$('.delete_message-link-js').live('click', function(e) {
    e.preventDefault();
});

$('.item-message').live('click', function(e){ //.item-message - tr lass
    window.location = $(this).find(".show_message-link").attr("href");
});

But when i change .live() to .on()

// click row opens message, unless you click delete
$('.delete_message-link-js').on('click', false );

$('.item-message').on('click', function(e){
     window.location = $(this).find(".show_message-link").attr("href");
  });

i have a bug in Firefox . When I click at .delete_message-link-js link in table row i get a error

prompt aborted by user
throw Components.Exception...by user", Cr.NS_ERROR_NOT_AVAILABLE);

But code works in Safari.

What i doing wrong?

Thanks and sorry for my english

like image 932
gnatok86 Avatar asked Jan 18 '23 23:01

gnatok86


1 Answers

Read the documentation:

http://api.jquery.com/on/

From there we can see that the on equivalent of this:

$('.delete_message-link-js').live('click', function(e) {
    e.preventDefault(); 
});

is:

$( document ).on( "click", ".delete_message-link-js", function(e){
    e.preventDefault(); 
});

And so on.

You could also read the source code to see how live is transformed into on:

live: function( types, data, fn ) {
    jQuery( this.context ).on( types, this.selector, data, fn );
    return this;
}

this.context is document (the default context as you did not give any context) and this.selector is '.delete_message-link-js' for the above example.

like image 170
Esailija Avatar answered Jan 25 '23 03:01

Esailija