0

I have a button on my page which, when clicked, listens for another button being clicked and then performs an AJAX call. For example:

$( 'button#a' ).click( function() {

    $( 'button#b' ).click( function() {
        // Perform an AJAX call here.
    });

});

Here is a demo of my code so far:

DEMO

I want to disable the ajax call functionality if a third button button#c is clicked at any time during the time that the page is loaded. I'm at a loss at how this can be done. Hoping someone can suggest an approach.

1
  • In the demo, click on button a and then button c and then button b. The AJAX call fires. I don't want that because button c was clicked. Commented Nov 19, 2014 at 18:30

3 Answers 3

1

Should not code click event inside click, but after modifying in your code.

var third_click= false; 
$(document).ready(function() {
    $( 'button#c' ).click( function() {
        third_click = true;
    })

    $( 'button#a' ).click( function() {

        $( 'button#b' ).click( function() {
            if( !third_click ) {
                // Perform an AJAX call here.
                alert( 'ajax call in progress' );
            } else {
                alert( 'Can\'t call ajax' );
            }
        });

    });
})

here is demo.

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

2 Comments

"Should not code click event inside click" - why is that?
@henrywright I think it's just not considered "nice coding"... But if you wanted to avoid that, you could just do more or less the same thing with button#a as it's done with button#c in the code above: jsfiddle.net/Niffler/qeojLmn4/2
0

What if you used button a to hide button b until a is clicked and then show button b and so fourth until your results are displayed as you with.

example:

  $('.btnB').hide();

 $('.btnA').on('click', function() {
  $('.btnB').toggle();
});

Unless it needs to all display at one time this method wouldn't be able to work in that type of situation

Comments

0

You can just remove the click events from #a and b# when clicking on #c with:

$('#c').click(function () {
    $('#a,#b').off('click')
})

jsFiddle example

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.