newbiedev
newbiedev

Reputation: 23

Run a click function only once within another function

So I'm running a function when someone clicks on an element with a certain class name (10 of these classes). Then within that function I have another click listener for elements with another class name (another 10). I want this second click function to only occur once after that first click.

So ideally someone would click something from a set of 10, I'd then pull data from that and apply it when someone clicks an element from another set of 10. And then in order to click that second set of 10 they will have to click something from the first set again.

I'm having trouble pulling that off and I've tried some sort of .one implementation.

 $('.words_col .wrap').click(function(){
   theFunction(this)
 })

Then

 function theFunction(e) {
   $('.examples_col .wrap').click(function(){
     //allow only once.
   })
 }

Upvotes: 0

Views: 390

Answers (2)

Fresheyeball
Fresheyeball

Reputation: 30015

$('.words_col .wrap').click(function(){
   theFunction(this);
 });

function theFunction(e) {
   var oncer = true;
   $('.examples_col .wrap').click(function(){
     if(!oncer){return false;}
     oncer = false;
     //allow only once.
   })
 }

I add this as an alternative to .one because you have more than one element being selected, and .one will allow one click on each, instead of one click total.

Upvotes: 1

adeneo
adeneo

Reputation: 318182

one() will attach the click only once:

$('.words_col .wrap').on('click', function(){
   $('.examples_col .wrap').one('click', function(){
       //works only once
   });
});

Upvotes: 0

Related Questions