Custom Script: Total Value for Child Table (upon changes)

Hi, I have tried numerous scripts and also sourced from many samples in this forum but I am still unable to ‘crack the code’.

Below script works but it will only calculate the ‘pfy_sales_target_total’ after save, I wanted calculate upon any changes in the child table. Can anyone really help me out? I am not a programmer and I have spent many days trying to fix this.

cur_frm.cscript.previous_target = function(doc) {   

cur_frm.cscript.calculate_total = function(doc){        
	doc.pfy_sales_target_total = 0;                               
	$.each((doc.previous_fiscal_year_group_sales_target || []), function(i, d) {       
		doc.pfy_sales_target_total += d.previous_target;

cur_frm.cscript.calculate_total_amount = function(doc,cdt,cdn){     

P.S. I know using cur_frm.cscript isn’t recommended, but I have no other choices; as I mentioned above, I am not a programmer. This is really the best I can do

Try like this.