0

hi i need to create a form where when i put a batch_num, i would like that the other 2 input text in the form, autofill with materialname and materialCode taken from a query/array i found this but it give me the error that autocomplete function doesn't exist:

Uncaught TypeError: $(...).autocomplete is not a function

How can i fix the error or make the autocomplete input?

$(function() {
var users = [{
    "email": "[email protected]",
    "name": "marie"
}, {
    "email": "[email protected]",
    "name": "miss"
}];

function handleAutocomplete(term) {
    // use 'term' for custom filtering etc.
    var options = $.grep(users, function(e){ return e.name.startsWith(term); });
    return options;
}
$("#name").autocomplete({
    minLength: 0,
    source: function(request, response) {
        var name = request.term;
        var data = handleAutocomplete(name); /* get answers from somewhere.. */
        response(data);
    },
    focus: function(event, ui) {
        $("#name").val(ui.item.name);
        return false;
    },

    select: function(event, ui) {
        $("#name").val(ui.item.name);
        $("#email").html(ui.item.email);
        return false;
    }
}).autocomplete("instance")._renderItem = function(ul, item) {
    return $("<li>")
        .append("<a>" + item.name + "<br>" + item.email + "</a>")
        .appendTo(ul);
};
});
1
  • 1
    The error suggests that you didn't include the JavaScript file containing the autocomplete functionality into your page. Commented May 12, 2021 at 10:11

1 Answer 1

2

You should not use <a> in your Item Rendering. Please see: https://jqueryui.com/autocomplete/#custom-data

I tested your code, and it is working properly when you add the jQuery UI:

$(function() {
  var users = [{
    email: "[email protected]",
    name: "marie",
    label: "marie",
    value: "[email protected]"
  }, {
    email: "[email protected]",
    name: "miss",
    label: "miss",
    value: "[email protected]"
  }];

  function handleAutocomplete(term) {
    // use 'term' for custom filtering etc.
    var options = $.grep(users, function(e) {
      return e.name.startsWith(term);
    });
    return options;
  }

  $("#name").autocomplete({
    minLength: 0,
    source: function(request, response) {
      var name = request.term;
      var data = handleAutocomplete(name); /* get answers from somewhere.. */
      response(data);
    },
    focus: function(event, ui) {
      $("#name").val(ui.item.name);
      return false;
    },

    select: function(event, ui) {
      $("#name").val(ui.item.name);
      $("#email").html(ui.item.email);
      return false;
    }
  }).autocomplete("instance")._renderItem = function(ul, item) {
    return $("<li>")
      .append("<div>" + item.name + "<br>" + item.email + "</div>")
      .appendTo(ul);
  };
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div>
  <label>Name:</label><input id="name" type="text">
  <div>Email: <span id="email"></span></div>
</div>

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

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.