I’m using the javascript remove action on a front end form to try to trigger a function that recalculates a sum of values from multiple fields. As you can see in the following code:
acf.add_action('remove', function($el) {
if (!$el.prevObject.hasClass("acf-clone")) {
var currenttab = $el.parents('.acf-field-repeater').attr('data-name');
calculate_subtotal(currenttab);
}
});
This function cannot get the values from the fields which are needed to calculate the subtotal.
Only when using a setTimeout, calculate_subtotal() can get the values.
acf.add_action('remove', function($el) {
if (!$el.prevObject.hasClass("acf-clone")) {
var currenttab = $el.parents('.acf-field-repeater').attr('data-name');
setTimeout(function(){
calculate_subtotal(currenttab);
}, 275);
}
});
Any idea why this might be happening?
It might help to know what’s in calculate_subtotal()
Also, what is the result of the calculation done in the first case? Is it not working or is it still adding the value of the row being removed?
I just solved it myself:
Because of the animation that happens on the removal of a repeater row, there is a 250ms delay before the values are removed.
by using .on('remove', function())
from jquery-ui the problem is gone.
here is my working code:
$j("." + currenttab + i).on("remove", function () {
calculate_subtotal(currenttab);
})
Thank you for your help!
I was going to say that most likely the field hasn’t been removed completely yet. I had a similar problem with building a JS app to modify repeater rows. It would delete the existing rows and then add new ones. The problem was that I was adding new rows and attempting to add values before the existing rows were actually removed which cause the removed of the values that were just added.