1

I'm using Jquery Autocomplete based on: http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/. I am able to select a value from the textbox and add it to a list as in the example. Is it possible to add the selected value to a hidden field? For example <input type='hidden' value='value 1,value 2, value 3' name="SelectedValue" id="SelectedValue"/>

3 Answers 3

1

To add more than one value to a hidden field:

var hdnValue = $('hdnFieldName').val();
$('hdnFieldName').val(hdnValue +','+selectedValue);

Have a look at string.join as well.

Sign up to request clarification or add additional context in comments.

Comments

1
$('input#suggest').result(function(event, data, formatted) {
   $("#SelectedValue").val( $("#SelectedValue").val() + "," data );
});

Look over autocomplete documentation for details

Comments

0

Don't re-invent the wheel. Just use this piece of jQuery. For each autocomplete input field it creates a hidden field using the original input field's name. In that hidden field the autocompleted key is stored. On submit the keys will be submitted. You don't have to change anything to your backend.

http://www.petefreitag.com/item/756.cfm

$('input.YourClassName').each(function() {
    var autoCompelteElement = this;
    var formElementName = $(this).attr('name');
    var hiddenElementID  = formElementName + '_autocomplete_hidden';
    /* change name of orig input */
    $(this).attr('name', formElementName + '_autocomplete_label');
    /* create new hidden input with name of orig input */
    $(this).after("<input type=\"hidden\" name=\"" + formElementName + "\" id=\"" + hiddenElementID + "\" />");
    $(this).autocomplete({source:'employee-search-json.cfm', 
        select: function(event, ui) {
            var selectedObj = ui.item;
            $(autoCompelteElement).val(selectedObj.label);
            $('#'+hiddenElementID).val(selectedObj.value);
            return false;
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.