Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .one() with .live() jQuery

Tags:

jquery

I was using the live() function:

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

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()?

like image 505
Titan Avatar asked Sep 26 '10 01:09

Titan


People also ask

How can use live method in jQuery?

Use the on() method instead. The live() method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live() method will work for both current and FUTURE elements matching the selector (like a new element created by a script).

What is difference between BIND and live in jQuery?

In short: . bind() will only apply to the items you currently have selected in your jQuery object. . live() will apply to all current matching elements, as well as any you might add in the future. The underlying difference between them is that live() makes use of event bubbling.

What is the difference between on and live in jQuery?

on() method: This method attaches events not only to existing elements but also for the ones appended in the future as well. The difference here between on() and live() function is that on() method is still supported and uses a different syntax pattern, unlike the above two methods.


1 Answers

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.

like image 167
egoard Avatar answered Oct 12 '22 01:10

egoard