0
votes

I'm trying to do the following (amongst many other attempts), and have validated that #myVar# holds data and is current in the scope, but nothing works. Any ideas how to populate a text input in Coldfusion? Many thanks in advance!

<cfoutput>#myVar#</cfoutput>
<script>document.getElementById("text1").value = #myVar# </script>
3

3 Answers

1
votes

Is your value a string or an integer? If its a string you will need wrap the value in quotes.

Additionally you will want to use the jsStringFormat() function to ensure any quotes are escaped.

<script>document.getElementById("text1").value = "#jsStringFormat(myVar)#"; </script>
2
votes

The easiest way is with the value attribute of an input tag.

<form>
<input name "fred" value="flinstone">
etc

If you want to populate it dynamically, use cfoutput.

<cfset myVar = "flinstone">
<cfoutput>
<form>
<input name "fred" value="#myVar#">
etc

or is that not the question?

2
votes

Your closing </cfoutput> is too early, where you're trying to output the value inside your javascript should also be wrapped in the cfoutput.

<cfoutput>
    #myVar#
    <script>document.getElementById("text1").value = "#myVar#";</script>
</cfoutput>