J. Chavez J. Chavez - 5 months ago 28
jQuery Question

Send Value From jQuery Autocomplete To HiddenField

I'm trying to Send the Current Value of AutoComplete jQuery to a HiddenField on ASP

Hidden Field:

<asp:HiddenField ID="hidden" runat="server" />


When the Page Loads I only assign the HiddenField Value to some TextBox:

Protected Sub PrepareSession(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim hid = hidden.Value
Requester_NumEmpleado_TB.Text = hid


The is the jQuery code:

<script type="text/javascript">
$("document").ready(function() {
$("#Requester_NumEmpleado_TB").autocomplete({
source: 'Search_VB.ashx',
autoFocus: true,
select: function(event, ui) {

}
});
$("#<%=hidden.ClientID %>").val("20020177") //this works but i want to send the Autocomplete Value, not the "20020177"
});
</script>


This works for a hardcoded value, but I want to send the value from the Autocomplete, not the "20020177". How do I do that?

Answer

All you need to do is retrieve the value in the autocomplete field in the select callback, and assign it to the value of the hidden field:

<script type="text/javascript">
$("document").ready(function() {
    $("#Requester_NumEmpleado_TB").autocomplete({
        source: 'Search_VB.ashx',
        autoFocus: true,
        select: function(event, ui) {
            var autocomplete_value = $("#Requester_NumEmpleado_TB").val();
            $("#<%=hidden.ClientID %>").val(autocomplete_value);
        }
    });
});
</script>
Comments