Your error message is entirely accurate: "The value + (indx + 1) + cannot be converted to a number."
Your variable rc.qGetAP.ID is a query column (effectively an array), and you are trying to index it with a string (as indicated by the single quotes), which CF is attempting to convert into a row number, but it cannot translate the text + (indx + 1) + into a number.
The problem here is less the code and more a matter of understanding what's going on:
JavaScript (which is what this is, not jQuery) occurs in the browser whilst ColdFusion occurs on the server.
These are two separate and distinct environments.
They are not interleaved in the way you are trying to do - they interact only over HTTP requests/responses (or via WebSockets/etc).
In brief:
- The browser asks for a page using a HTTP request.
- The CF server compiles and executes CFML code, which outputs text.
- That text is sent via HTTP response to the client.
- The browser then interprets that text as HTML/CSS/JS/etc - completely apart from the action of the CFML code being processed.
For a more complete explanation on client/server and HTTP request/response, here is a blog post discussing the subject.
Once you have understood how this process works, the solution is to either:
1) generate all the relevant JS variables from CF first (so your Ids are translated to a JS array), and you have a single line of code to call the JS function CloneAvgPopulationItem inside a JS loop.
For example:
<script type="text/javascript">
var AvgPopIds = <cfoutput>#serialiseJson(ValueArray(rc.qGetAP.ID))#</cfoutput>;
for (var index = 0; index < avgPopulationRecordsCount; index++) {
CloneAvgPopulationItem(index, AvgPopIds[index]);
}
</script>
2) resolve the loop on the CF side, so you get multiple JS lines calling the CloneAvgPopulationItem function.
For example:
<script type="text/javascript">
<cfoutput query="rc.qGetAP">
CloneAvgPopulationItem(#rc.qGetAP.CurrentRow#,'#JsStringFormat(rc.qGetAP.Id)#');
</cfoutput>
</script>
It is important to state: do not just use these code examples but read and understand the article linked above about where JavaScript and ColdFusion run - the whole HTTP request/response process is an important one to understand and will avoid you encountering similar problems in future.