0

I have a jQuery function that will be run when user click a submit button of form is submitted

FUNCTION #1
jQuery(function($){

    $('#filter').submit(function() {
        var filter = $('#filter');
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(), // form data
            type:filter.attr('method'), // POST
            beforeSend:function(xhr){
                filter.find('button').text('Filtering...'); // changing the button label
            },
            success:function(data){
                filter.find('button').text('Filter'); // changing the button label back
                $('#response').html(data); // insert data
            }
        });
        return false;
    });
});


<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">

I have other script with javascript that run when user click on link with id "link-id".

FUNCTION #2

$div1 = $(".div1");
$div2 = $(".div2");

$div1.hide()
$div2.hide()

function showDiv(){
   if(document.getElementById('chk1').checked){ 
       $div1.show()
   } else {
       $div1.hide()
   }
   if(document.getElementById('chk2').checked){
       $div2.show()
   } else {
       $div2.hide()
   }
}
$('#link-id').on('click', showDiv)  

<a id="link-id" class="btn btn-primary" href="javascript: void(0)">Go ahead</a>

I'd like to add and if and else in the FUNCTION #2 that:

  1. if jQuery 'FUNCTION #1' is not submitted (run), javascript 'FUNCTION #2' do something (current code);
  2. if user has already click one time on button to run 'FUNCTION #1', the 'FUNCTION #2' shall do other thing.

I'm not expert with javascript code. Is there a way to made this check?

1 Answer 1

1

If I'm understanding you correctly the two functions are called from different event which means at different times.

If the scripts are linked (by integrating both in the html) or in one:

Then you could add a global variable that changes in function 1 and is being checked in function 2.

Declaration:

let functionOneWasExectuted = false;

In function 1:

functionOneWasExectuted = true;

In function 2:

if (functionOneWasExectuted) {
  // code function 1 has been executed before
} else {
  // code function 1 has not been executed before
}

If the 2 scripts are not linked in any way let me know and I'll post a different solution :)

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

3 Comments

Pretty neat solution, I'd go for this as well :)
Thanks a lot for your reply @marvin. The script are different, but contained in the same html code. For declaration, it shall be added on the first jquey code?
@marvin I update the code like this: FUNCTION #1 let functionOneWasExectuted = false; jQuery(function($){ and at the end of functions: return false; functionOneWasExectuted = true; And added if and else on functions #2, but doesn't work.

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.