I have a simple function that appends the shipping details, to billing details in my woocommerce checkout. However I want this function to stop firing once a class has ben added to a div.
function billingclick() {
$('#billing_first_name').val($('#shipping_first_name').val());
$('#billing_last_name').val($('#shipping_last_name').val());
};
$('.wpmc-ripple.wpmc-billing').on('click', function(e) {
billingclick();
});
I can’t use the one click function, as validation breaks this.
Is there a way I can stop the function if the div class is added?
e.g if .wpmc-step-shipping has the class current, stop the billingclick() function.
if (!$(".wpmc-step-shipping").hasClass("current")) {
// function here
}
Thank you
3
Answers
Your code should look like this :
Perform the
if condition
first inside your function.It seems like you’re trying to remove the click handler. While this is possible, you need to know exactly when the class is added to the relevant element, which is not always possible, nor simple.
A easier solution is to instead place your
if
condition within thebillingclick()
function and only update theval()
of the required elements when the class is not present. Try this:As the other answers said, you can perform that check in your click handler:
You could also remove the event handler on
$('.wpmc-ripple.wpmc-billing')
$('.wpmc-ripple.wpmc-billing').off('click')
You could also disable the ‘.wpmc-ripple.wpmc-billing’ control.