I have on my page a counter (red circle) that adds up all the numbers (red boxes) in my repeater.It worked fine until I added a selection with a dropdown menu (red arrow).
I did an OnChange in the code but the counter gives the correct number not when I click on the selection but when I leave that selection.
What am I doing wrong ?
![](https://static.wixstatic.com/media/a27d24_739c3441762e405e90214f442a9638f2~mv2.png/v1/fill/w_980,h_708,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/a27d24_739c3441762e405e90214f442a9638f2~mv2.png)
Here is the code:
$w.onReady(function () { $w("#dataset1").onReady( function() { $w("#dropdown11").onChange(async (event, $w) => { let initialValue = 0; let sum = $w("#listRepeater").data.reduce( (accumulator, currentValue) => accumulator + Number(currentValue.total) ,initialValue ); $w("#text53").text = sum.toString() }) }) })
Thanks
I'll look into this option to improve my site, but for now, putting the
$w("#text53").text=sum.toString();
Inside the setTimeout after the Reduce works perfectly.
Here is the working code once modified:
$w.onReady(function () { $w("#dataset1").onReady( function() { $w("#dropdown11").onChange(async (event, $w) => { setTimeout(() => { let initialValue = 0; let sum = $w("#listRepeater").data.reduce( (accumulator, currentValue) => accumulator + Number(currentValue.total) ,initialValue ); $w("#text53").text=sum.toString(); }, 1000); }) }) })
Thanks guys
Assuming that this dropdown works as dataset filter (you haven't mentioned it), I'd try to run the filter by code (and not via the the editor ui).
This way you will have control over the action sequence, and you will be able to run the reduce method AFTER the dataset got filtered and not before.
https://www.wix.com/velo/reference/wix-dataset/dataset/setfilter
I think your fundamental problem is with how onChange operates. At the time the handler fires the repeater and drop down side effects will not actually have changed. One possible solution is to wrap your reduce statement in a setTimer call and delay the update for 500ms or 1sec to allow any repeater changes to complete. There are other approaches but this might be the easiest one to get started and test my theory :-)