sweetbuttercup
asked on
Javascript var to coldfusion
I have the following within a javascript funtion
var variable1= customeridvalue;
When I show the "variable1" throught an alert it gives the value that I need. I want to pass that variable1 value to the following page using a hidden field. I don't know how to dump the value of that variable. I need to do something like this to work:
<input type="hidden" name="customerid" value="<cfoutput>#variable 1#</cfoutp ut>"> (this doesn't work, of course)
How do I set the value=variable1
var variable1= customeridvalue;
When I show the "variable1" throught an alert it gives the value that I need. I want to pass that variable1 value to the following page using a hidden field. I don't know how to dump the value of that variable. I need to do something like this to work:
<input type="hidden" name="customerid" value="<cfoutput>#variable
How do I set the value=variable1
With javascript you can use CF variables, but not the other way around...
so if you want to assign a value from coldfusion to a javascript variable, that'll work, then you can pass that variable to a hidden field to submit to the next page....
if this is your hidden field, you'd want to do something like this...
<cfset customeridvalue = "Whatever">
<script>
Function Something(){
var variable1= <cfoutput>'#customeridvalu e#'</cfout put>;
document.getElementById("c ustomerid" ).value = variable1;
}
</script>
with a hidden field like so....
<input type="hidden" name="customerid" id="customerid">
then it'll grab that value....
wouldn't it be easier though to just do this...
<cfset customeridvalue = "Whatever">
<cfoutput>
<input type="hidden" name="customerid" id="customerid" value="#customeridvalue#">
</cfoutput>
so if you want to assign a value from coldfusion to a javascript variable, that'll work, then you can pass that variable to a hidden field to submit to the next page....
if this is your hidden field, you'd want to do something like this...
<cfset customeridvalue = "Whatever">
<script>
Function Something(){
var variable1= <cfoutput>'#customeridvalu
document.getElementById("c
}
</script>
with a hidden field like so....
<input type="hidden" name="customerid" id="customerid">
then it'll grab that value....
wouldn't it be easier though to just do this...
<cfset customeridvalue = "Whatever">
<cfoutput>
<input type="hidden" name="customerid" id="customerid" value="#customeridvalue#">
</cfoutput>
ASKER
mkishline: I tried your solution but get the output of document.write('value="' + variable1 + '"'); /> printed on the screen and it doesn't pass the value
trailblazzyr55 I need to use the JS variable. I tried using CFSET to assign the vlue of the JS var to a CF var but doesn't work
trailblazzyr55 I need to use the JS variable. I tried using CFSET to assign the vlue of the JS var to a CF var but doesn't work
this form of assigning a CF value to a Javascript one will work...
<script>
Function Something(){
var variable1= <cfoutput>'#customeridvalu
document.getElementById("c
}
</script>
you have to call the funtion of course to run it, however if you want to do it when the page loads you can do this...
<script>
function init(){
var variable1= <cfoutput>'#customeridvalu
document.getElementById("c
}
window.onload = init();
</script>
you don't use the cfset's inside the javascript, but you can output the CF vars in javascript to use their values, however since coldfusion is run on the server and javascript on the client, the cf variables are always populated first, the javascript can just access those values that are outputed from CF. this is why you have to use the cfoutput inside the javascript... So you can't use cfset to assign a javascript value to a cf variable becuase the cf is always processed first...
If you want to use a value from javascript, assign that value to a hidden field, submit the form and assign that form variable then to a cf value. It requires a submit because you have to go back to the server where cf is processed.
make sense?
ASKER
My var is a JS var not a CF var. I just need to capture the value of the JS var and submit it to the second page as a hidden field or any other way.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mkishline : I think my original question was very confusing. I shouldn't have mixed up Coldfusion on all this to make your life easier. Thank you so much!!!
<input type="hidden" name="customerid" <script type="text/javascript">doc