pmillio
pmillio

Reputation: 1139

Jquery autocomplete only when hotkey is present

Hi i want to bring back some product and usernames, however they should only comeback when they "@" symbol has been typed first, how do i go about doing this?

Hi i managed to come up with this,

<script type="text/javascript">
    $(function () {
                $(".fateinp").autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            url: "WebServices/List.asmx/FetchUsers",
                            data: "{ 'product': '" + request.term + "' }",
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            dataFilter: function (data) { return data; },
                            success: function (data) {
                                response($.map(data.d, function (item) {
                                    return {
                                        value: item._Name
                                    }
                                }))
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert(textStatus);
                            }
                        });
                    },
                    minLength: 2
                });
    });
</script>

However whenever i add your suggestion;

var autocompleter = document.getElementById("some-element");

I keep getting autocompleter is null, forgive me if this is a novice question. Thanks

Upvotes: 0

Views: 289

Answers (1)

Yuck
Yuck

Reputation: 50855

Wrap the call to AJAX for autocompleting (or displaying if they've already been cached) using something like this.

var autocompleter = document.getElementById("some-element");

if (autocompleter.value.startsWith("@")) {
  // invoke AJAX here
}

Upvotes: 2

Related Questions