.one () does not work, as jquery.com suggests for first use

I have exhausted my research on using .one () for my needs and made it work correctly for an instance of the class selector, where it is displayed only once. But on jQuery.com it is indicated that the following will work, and if you click once, it will not start again if another item with the same class selector is clicked. However, this does not work for me. I have and need the same specific class selector on the same page.

Here is the code that jQuery.com gives as an example:

$("#foo").one("click", function() { alert("This will be displayed only once."); }); $("body").one("click", ".foo", function() { alert("This displays once for the first .foo clicked in the body."); }); 

the second above does not seem to work, as they say. Does anyone know how to actually make this work. Where can I have .foo in several places, but the function shows only once, after clicking on one of .foo. And when another .foo is clicked, it will no longer run this function? Thanks so much for any help / suggestions.

+4
source share
2 answers

As explained in this comment, this is . When you read correctly (as dystroy says , it is not written perfectly), the handler will be launched only once, for the first element that corresponds to the selector.

To have this fire no more than once per .foo , you will have to do it yourself (note that we switched from one() to on() and processed the speed limiting ourselves.

 $("body").on("click", ".foo", function() { var self = $(this); if (!self.data('fired')) { self.data('fired', true); alert("This displays once for each .foo clicked in the body."); } }); 

http://jsfiddle.net/Zdqpp/1/

0
source

you can try with this:

 $(function(){ $(".foo").one("click",function(event) { alert("This displays once for the first .foo clicked in the body."); }); }); 

this will hit only .foo and alert for one time . if you click it again without rebooting, a warning will not be displayed.

script: http://jsbin.com/eluquq/1/edit

0
source

All Articles