As part of creating a form editor in Coldfusion system, I am keen on leveraging inline script tags for adding Javascript event listeners dynamically. Here is how my code structure looks like:
<cfoutput>
...
<cfloop>
<cfset FormElementPK = rc.formElements.getRow(i)["FormElementsPK"]>
<script>
//Create a listener for the question text editor to check for errors on blur
var #toScript(FormElementPK, "jsFormElementPK")#;
var inputField = document.getElementById("##question" + jsFormElementPK + "TextEditor");
$("##question" + jsFormElementPK + "TextEditor").on('blur', function(e) {
alert("I am a test message!");
checkQuestionTextForErrors(inputField.id);
});
</script>
...
</cfloop>
...
</cfoutput>
EDITED: Added correct concatenation by credit of Will Belden
My goal with this code is to ensure that whenever a textarea element loses focus (onblur event in jquery), I can detect it for each item in my form. However, the current implementation is not achieving the desired result.
Interestingly, when I eliminate the FormElementPK variable and statically reference the "#questionXTextEditor" variable such as "question1TextEditor", the functionality works fine. But due to the need for dynamic generation of questionTextEditor elements, I believe using the FormElementPK variable from Coldfusion is essential.