1

When a link is clicked on my site the Javascript code below is executed, if the condition is true it will display an alert dialog. When the user selects the OK button in the alert dialog the block of code is executed again.

So the alert closes, the code below is executed for a second time and the alert dialog is displayed again. When the used selects the OK button on the alert dialog the second time the alert dialog is closed for good.

How can I prevent the code below being executed twice?

$("#my-button").click(function() {
    var login = someVar;
    if(!someVar || someVar == ''){
        $('.close-reveal-modal').click(); 
        alert(myMessage);
    }
});
9
  • 2
    please post the html or try to reproduce it on jsfiddle Commented Jul 31, 2013 at 10:30
  • 3
    Does #my-button also have a class of .close-reveal-modal, by any chance? Commented Jul 31, 2013 at 10:31
  • @evilbhonda - wouldn't that send it into an infinite loop of alerts? Commented Jul 31, 2013 at 10:35
  • 1
    @RobH maybe, I'd need to check. But from the limited code that's provided, that's the part that jumped out at me Commented Jul 31, 2013 at 10:37
  • Can you be adding the same code (click handler) twice? Commented Jul 31, 2013 at 10:41

4 Answers 4

1

Check if you are adding the click handler twice, maybe that is what is causing that behavior.

In that case remove one of them.

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

Comments

1

From the very limited information that's provided, this is all that I can think of as going wrong:

$('.close-reveal-modal').click();

This piece of code should have some kind of function which is executed to display a similar Alert Box.

A complete code would be more useful for a complete answer!

Comments

1

Might not have anything to do with that code at all. Check to make sure that your javascript file isn't being called twice in the same app.

Comments

0

From what we have here, I'm guessing that your .close-reveal-modal element is in #my-button (or is the same html node).

When you trigger the click on it (by $('.close-reveal-modal').click();), it also trigger the click on its parent node, so on #my-button too.

I can be wrong, we need the HTML part (a fiddle would be great) to validate my theory.

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.