Copy link to clipboard
Copied
Please help me to get the $("#txt").val(id); into coldfusion variable to do some sql query. Thanks
<body>
<script>
$(document).ready(function (){
$(".ap").click(function (){
var id = this.id;
$("#txt").val(id);
})
})
</script>
<div id="Test" class="ap">click</div>
<input id="txt" name="txt" value="" type="text">
</body>
Copy link to clipboard
Copied
If you're asking how to put a jQuery value into a CF variable, the only way I've done it is to use AJAX to submit the value to a .cfm page and set a session variable.
But there might be other ways to do it. This is the way I do it.
HTH,
^ _ ^
Copy link to clipboard
Copied
Hi WolfShade,
Thank you for the reply!
Could you please given a example of the code.
Copy link to clipboard
Copied
I can try. Very basic, bare-bones example. This assumes you are using jQuery 1.x.
setSV.cfm:
<cfswitch expression="#StructKeyExists(form,"yourVariable")#">
<cfcase value="true">
<cfset session.yourVariable = form.yourVariable />Success!
</cfcase>
<cfdefaultcase>
Failed.
</cfdefaultcase>
</cfswitch>
index.cfm:
<form id="yourForm" name="yourForm">
<input id="yourVariable" name="yourVariable" /> <button id="sendIt" type="button">Set Your Variable in CF</button>
</form>
<script src="jquery-1.11.2.min.js"></script>
<script>
$('#sendIt').on('click',function(){
switch($('#yourVariable').val().trim()){
case "":
alert("There must be a value to do this.")
break;
default:
$.post(
"setSV.cfm",
{yourVariable: $('#yourVariable').val().trim()}
).done(function(data){alert(data);});
break;
}
});
</script>
I have not tested the CF on this, but the form/jQuery is working.
HTH,
^ _ ^
UPDATE: Finally tested it on a local server. There are some typos, I am correcting them.
Copy link to clipboard
Copied
Hello,
Just following up to see if any of this was helpful.
V/r,
^ _ ^
Copy link to clipboard
Copied
<script type="text/javascript">
$(document).ready(function (){
$(".ap").click(function (){
var id = this.id;
$("#txt").val(id);
$("#myFormId").submit()
})
})
</script>
<body>
<div id="Test" class="ap">click</div>
<form id="myFormId" method="post">
<input id="txt" name="txt" value="" type="text">
</form>
</body>
<!--- Test it --->
<cfdump var="#form#" >