0

First of all, this is my first question here, so I want to Apologize in advance if it is repeated.

I have a piece of code like this:

$("#one").click(function(){ 
    randomNumber = Math.floor(Math.random()*6)+1;
    $("#two").click(function(event){
        console.log(randomNumber);
    });
});

First time everything is working fine, but second time i executed that It shows the first random number and then the second, etc.

3
  • 6
    Well, you're attaching a new click event handler to #two on each click of #one hence you see the console.log being executed multiple times. What behaviour are you trying to create? Commented Mar 9, 2016 at 10:00
  • You could use $("#two").one('click', handler); but surely you have better to do, depending what exact behaviour you are expecting, like e.g what are you expecting if clicking on #two before any click on #one??? And beware, here you are defining randomNumber on global scope Commented Mar 9, 2016 at 10:05
  • Well, you are right, I think I didn't explain the behaviour as well as I thought... The behaviour I expect is: 1. You click on #one and a random number is generated 2. You click on #two and the random number is printed, but only if the first button was clicked, If not, the second button does nothing. Hope this explain better what I am trying to do...thanks a lot Commented Mar 9, 2016 at 11:33

3 Answers 3

1

When click on #one happens, randomNumber is generated and handler to the #two is assigned. This means that subsequent clicks on the #one add multiple handlers meaning that the code will be outputted more than once.

If you want to have only one handler you can do something like this

(function(){
     var randomNumber;
     $("#one").click(function(){
         randomNumber = Math.floor(Math.random() * 6) + 1;
     });

     $("#two").click(function(){
         if(randomNumber){
             console.log(randomNumber);
         }
     });
})(} 

EDIT: Added test for randomNumber, which means that it happens only after the first button was clicked. `

Sign up to request clarification or add additional context in comments.

2 Comments

Ok! Thank you so much, but I think I didn't explain correctly what I want to do... sorry :( The behaviour I expect is: 1. You click on #one and a random number is generated 2. You click on #two and the random number is printed, but only if the first button was clicked, If not, the second button does nothing. Hope this explain better what I am trying to do...thanks a lot
Ok! Now I see what I was doing wrong...Thank you very much, I was totally stuck on this...and it was easier than I thought! :D
1

The behaviour I expect is: 1. You click on #one and a random number is generated 2. You click on #two and the random number is printed, but only if the first button was clicked, If not, the second button does nothing.

You can use .off() method to remove previously bound event handlers. This will prevent the double or even multiple "shows the first random number and then the second, etc"

$("#one").on('click', function(){ 
    randomNumber = Math.floor(Math.random()*6)+1;
    $("#two").off('click').on('click', function(event){ // Preserve other event handlers if any.
        console.log(randomNumber);
    });
});

Comments

0

Try like this.

$(document).ready(function(){
    $("#one").click(function(){ 
    randomNumber = Math.floor(Math.random()*6)+1;
    $("#two").click(function(event){
        console.log(randomNumber);
    });
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.