tags:

views:

54

answers:

3

I was using the .live function

$('a.remove_item').live('click',function(e)

but I needed to change this to one() to prevent multiple clicks, however when I inject one of these elements after the page has loaded the one() listener does not fire.

How can I get one() to behave like live()?

Thank you!

+4  A: 

Try this:

$('a.remove_item').live('click',function(e) {
  if($(e.target).data('oneclicked')!='yes')
  {
    //Your code
  }
  $(e.target).data('oneclicked','yes');
});

This executes your code, but it also sets a flag 'oneclicked' as yes, so that it will not activate again. Basically just sets a setting to stop it from activating once it's been clicked once.

egoard
Good solution. I think it's better than JapanPro's because using the data function is more discrete than adding a class.
Kranu
this appears to work well thank you!
Alex Crooks
A: 

Try this

$('a.remove_item').live('click', function(e) {
    if(!$(this).hasClass('clicked'))
    {
      $(this).addClass('clicked');
      alert("dd"); // this is clicked once, do something here
    }
});​
JapanPro
this would work similar to the data function, however like Kranu said this is less discrete. Thank you anyway!
Alex Crooks
+2  A: 

Just use jQuery's .die() method in the handler:

Example: http://jsfiddle.net/Agzar/

$('a.remove_item').live('click',function(e) {
    alert('clicked');
   $('a.remove_item').die('click'); // This removes the .live() functionality
});​

EDIT:

Or if you only wanted to disable the event on a per-element basis, you could just change the class name since live() is selector-based.

Example: http://jsfiddle.net/Agzar/1/

$('a.remove_item').live('click',function(e) {
    alert('i was clicked');
    $(this).toggleClass('remove_item remove_item_clicked');
});​

This changed the class from remove_item to remove_item_clicked which could have the same styling. Now live() will not fire after the first click.

patrick dw