Resetting auto-suggest field after ajax post

wkhatch's Avatar

wkhatch

05 Mar, 2011 03:10 PM

I'm having some issues trying to reset a previously set up auto-suggest field after an ajax submission. Basically, I want the response handler to get the terms submitted on the previous request and add them to the auto suggest list for the next potential submission. ie, a user enters some terms, using auto suggest field, submits, and if they're new, they're returned and my function reinitializes auto suggest on the same field. I've got the terms coming back fine, but they're not getting added to the list. Here's the code I use on page load, to set up the field initially:

<%= javascript_tag do %>

    $(function(){
            var data = <%= @auto_suggest_json %>
            $("input#observation_tags").autoSuggest(
                data.tags, 
                {   selectedItemProp: "name_and_count", 
                    selectedValuesProp: "descriptor_name",
                    searchObjProps: "descriptor_name",
                    startText: "Type in some descriptive tags, separated by commas",
                    asHtmlID: "new_obs_tags"
                });
    });

<% end %>

And here's the relevant portions from the response handler:

var json_response = jQuery.parseJSON(e.responseText);

    $("ul.as-selections li.as-selection-item").each(function(){
        $(this).remove();
    });
    $("#new_obs_tags").val("");
    $("input#observation_tags").autoSuggest(
        json_response.tags, 
        {   selectedItemProp: "name_and_count", 
            selectedValuesProp: "descriptor_name",
            searchObjProps: "descriptor_name",
            startText: "Type in some descriptive tags, separated by commas",
            asHtmlID: "new_obs_tags"
        });

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac