Thomas Galue Thomas Galue - 6 months ago 7
Javascript Question

JQuery Autocomplete for various inputs text with same function

i have a problem with JQuery Autocomplete.

I have this code:

$(function() {
var aspecto = [
"homogéneo",
"heterogéneo",
];
$("#aspecto").autocomplete({
source: aspecto
});
});


it works perfect, but i want to use the same structure for others inputs only editing the function name and values of 'var', when i put the 2nd block i get nothing for all the input fields.

What's wrong with that?

Thanks anyway!

Answer

I may need to make some assumptions in order to help. First I assume the $("$aspecto") is a typo. I think you want either by id $("#aspecto") or by class $(".aspecto")

So if your html is:

<div>
  <label for="aspecto">aspecto: </label>
  <input id="aspecto" />
</div>
<div>
  <label for="not-aspecto">not aspecto: </label>
  <input id="not-aspecto" />
</div>

then your javascript should be:

$(function() {
   var aspecto = [
      "homogéneo",
      "heterogéneo",
    ];
    $("#aspecto").autocomplete({
      source: aspecto
    });

   var notAspecto = [
      "other thing",
      "or not other thing",
    ];
    $("#not-aspecto").autocomplete({
      source: notAspecto
    });
});

Here is a functioning example for you to play with: https://jsfiddle.net/np0m99w8/

Comments