2

I now have a working JavaScript autocomplete function, thanks to help from many of you. Now I want to make the function reusable. There are three variables that need to be specified for each instance of the function, as shown below. What I don't know how to do is instantiate this function with different values for these three vars.

Here is my HTML field:

<div class="ui-widget">
    Text or Value:
    <input type="text" id="dotmatch" />
</div>

And here is the JavaScript code which I want to keep in its own .js file:

var matchFieldName = 'dotmatch';
var resultFieldName = 'dotnumber';
var lookupURL = "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList";

$(function() {
$('#' + matchFieldName).autocomplete({
    source: function(request, response) {
        $.ajax({
            type: "POST",
            url: lookupURL,
            contentType: 'application/json',
            dataType: "json",
            data: JSON.stringify({ prefixText: request.term, count: 20 }),
            success: function(data) {
                var output = jQuery.parseJSON(data.d);
                response($.map(output, function(item) {
                    return {
                        label: item.Label + "(" + item.Value+ ")",
                        value: item.Value
                    }
                }));
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    },
    minLength: 2,
    select: function(event, ui) {
        $('#' + resultFieldName).val(ui.item.value);
        return ui.item.label;
    }
});

});

2 Answers 2

2

insin was close. The solution I worked out this morning is;

function AutoComplete(matchFieldName, resultFieldName, lookupURL) {
    $('#' + matchFieldName).autocomplete({
        source: function(request, response) {
            $.ajax({
                type: "POST",
                url: lookupURL,
                contentType: 'application/json',
                dataType: "json",
                data: JSON.stringify({ prefixText: request.term, count: 20 }),
                success: function(data) {
                    var output = jQuery.parseJSON(data.d);
                    response($.map(output, function(item) {
                        return {
                            value: item.Value,
                            label: (item.Label == item.Value) ? item.Label : item.Label + "(" + item.Value + ")"
                        }
                    }));
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 2,
        select: function(event, ui) {
            $('#' + resultFieldName).val(ui.item.value);
        }
    });
}

On the web page:

<div id="AutoSuggest">
    DOT Job Title or Number:
    <input type="text" id="dotmatch" style="width:300px;" />
</div>

And on the web page, after the tag:

<script type="text/javascript" src="js/DOTAutocomplete.js"></script>

<script type="text/javascript">
    $(function() {
        AutoComplete("dotmatch", "dotnumber", "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList");
    });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to add .data( "autocomplete" ) to this. If you can assist, please see: stackoverflow.com/questions/8330096/…
0

It looks like you're using jQuery, so you might want to implement it as a plugin.

(function($) {
  $.fn.bobsAutocomplete = function(resultFieldName, lookupURL) {
    this.autocomplete({
      source: function(request, response) {
          $.ajax({
              type: "POST",
              url: lookupURL,
              contentType: 'application/json',
              dataType: "json",
              data: JSON.stringify({prefixText: request.term, count: 20}),
              success: function(data) {
                  var output = jQuery.parseJSON(data.d);
                  response($.map(output, function(item) {
                      return {
                          label: item.Label + "(" + item.Value+ ")",
                          value: item.Value
                      }
                  }));
              },
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                  alert(textStatus);
              }
          });
      },
      minLength: 2,
      select: function(event, ui) {
          $('#' + resultFieldName).val(ui.item.value);
          return ui.item.label;
      }
    });
    return this;
  };
})(jQuery);

Usage:

$("#dotmatch").bobsAutocomplete("dotnumber", "/AutoSuggestJSTest/AutoSuggest.asmx/DOTList");

1 Comment

Can you give me a bit more of a clue about the usage? What I would like to see is two instances. My current instance looks like: <div class="ui-widget"> Text or Value: <input type="text" id="dotmatch" /> $("#dotmatch").bobsAutocomplete </div> <hr /> Result Value:<input type="text" id="dotnumber" style="width: 100px" />

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.