Please find example in Go Playground. Comment (//) the line onRuleAreaChange(document.getElementById('ruleArea0')); to see the problem
I have some JavaScript that changes drop-down (SELECT) lists on forms based upon other drop-down list selections. To initialise the drop-down lists I call the onChange function I have written. On initialisation of the form, I call the onChange function for each row of my form.
The following lines of code range over each row and makes the call for that row thus setting up the configuration of the other fields in the whole row.
// Initialise all the rows
{{- range $i,$v := .}}
onRuleAreaChange(document.getElementById('ruleArea{{$i}}'));
{{- end}}
Go tells me that I have a {{range}} ending in different contexts, I'm not sure why - I looked all over the place for some missing terminator.
The fix for this was to simply add a line that is identical to the first line generated in the range loop. i.e. initialise the first row (which I always have) before ranging over the whole set as follow:
// Initialise all the rows
onRuleAreaChange(document.getElementById('ruleArea0'));
{{- range $i,$v := .}}
onRuleAreaChange(document.getElementById('ruleArea{{$i}}'));
{{- end}}
Can someone see why adding this extra line resolves the problem?
The final output that works is:
onRuleAreaChange(document.getElementById('ruleArea0'));
onRuleAreaChange(document.getElementById('ruleArea0'));
onRuleAreaChange(document.getElementById('ruleArea1'));
The output I would like to get but can't because of the error is:
onRuleAreaChange(document.getElementById('ruleArea0'));
onRuleAreaChange(document.getElementById('ruleArea1'));