Skip to main content
Participant
June 17, 2019
Question

How to get jquery value in coldfusion variable OR how to get input text value in cold fusion variable

  • June 17, 2019
  • 2 replies
  • 2494 views

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>

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    June 20, 2019

    <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#" >

    WolfShade
    Legend
    June 17, 2019

    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,

    ^ _ ^

    Participant
    June 18, 2019

    Hi WolfShade,

    Thank you for the reply!

    Could you please given a example of the code.

    WolfShade
    Legend
    June 18, 2019

    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.