Solution 1 :

const LANGUAGE_BY_LOCALE = {
  af_NA: "Afrikaans (Namibia)",
  af_ZA: "Afrikaans (South Africa)",
  af: "Afrikaans",
  ak_GH: "Akan (Ghana)",
  ak: "Akan",
  sq_AL: "Albanian (Albania)",
  sq: "Albanian",
  am_ET: "Amharic (Ethiopia)",
  am: "Amharic",
};

const setup = () => {
  fillDataList();

};
const fillDataList = () => {
  const localeOptions = document.querySelector('#locale-options');
  Object.keys(LANGUAGE_BY_LOCALE).forEach((key) => {
    let option = document.createElement('option');
    option.setAttribute('data-value', key);
    option.value = LANGUAGE_BY_LOCALE[key];
    localeOptions.appendChild(option);
  });
};


//load
window.addEventListener('load', setup);
<label for="locale-choice">Locale:</label>
<input list="locale-options" id="locale-choice" name="locale-choice">

<datalist id="locale-options"></datalist>

Solution 2 :

You can use oninputevent listener and filter the languages list based on the text entered:

var LANGUAGE_BY_LOCALE = {
  af_NA: "Afrikaans (Namibia)",
  af_ZA: "Afrikaans (South Africa)",
  af: "Afrikaans",
  ak_GH: "Akan (Ghana)",
  ak: "Akan",
  sq_AL: "Albanian (Albania)",
  sq: "Albanian",
  am_ET: "Amharic (Ethiopia)",
  am: "Amharic",
  
}

const matches = document.getElementById('matches');
const locale = document.getElementById('locale');

locale.addEventListener('input', () => {
  let text = event.target.value;
  let re = new RegExp(text.replace('(', '\('));
  let selLanguages = Object.values(LANGUAGE_BY_LOCALE).filter(lan => re.test(lan)).map(text => `<li>${text}</li>`).join('');
  
  matches.innerHTML = selLanguages;
});

matches.addEventListener('click', () => {
  locale.value = event.target.textContent;
  matches.innerHTML = '';
});
li {
  list-style-type: none;
  margin-left: 3em;
}
li:hover {
  background-color: #eee;
}
<form>
  <label>Locale: <input type="text" name="locale" id="locale" /></label>
  <div id="matches">
  </div>
</form>
 

Problem :

I’m building an HTML page with a text field for the user to type in any given language. I have a javascript file of world languages that I need the text field to autocomplete from. I have linked the javascript file to the HTML page but I cannot get the autocomplete to pull from the javascript file.

How can I autocomplete a dropdown menu by pulling data from a javascript file?

var LANGUAGE_BY_LOCALE = {
  af_NA: "Afrikaans (Namibia)",
  af_ZA: "Afrikaans (South Africa)",
  af: "Afrikaans",
  ak_GH: "Akan (Ghana)",
  ak: "Akan",
  sq_AL: "Albanian (Albania)",
  sq: "Albanian",
  am_ET: "Amharic (Ethiopia)",
  am: "Amharic",
  
  ... and so on
<p>Locale: <input type='text' value='uzb' /></p>
      <div><ul>
        <li>Uzbek (Arabic)</li>
        <li>Uzbek (Arabic, Afghanistan)</li>
        <li>Uzbek (Cyrillic)</li>
        <li>Uzbek (Cyrillic, Uzbekistan)</li>
        <li>Uzbek (Latin)</li>
        <li>Uzbek (Latin, Uzbekistan)</li>
        <li>Uzbek</li>
      </ul></div>
    <hr />
    <form>
      <label for="locale">Locale:</label><input type="text" name="locale" id="locale" />
      <div id="matches">
      </div>
    </form>
      <script src="locales.js" type="text/javascript"></script>
      <script src="jquery-6.14.2.min.js"></script>

Comments

Comment posted by Addis

“I have linked the javascript file to the HTML page”- I don’t see any links, it’s just the

Comment posted by jayiscoding

the javascript file name is called “locales.js” which is the block of code at the top. I have linked that file at the second-to-last line in the HTML. What should happen is that when the user begins typing a language, such as Uzkeb, as they type “Uzb…” all possible Uzbek language options would dropdown below the text field.

Comment posted by Addis

Can you post the file (locals.js)?

Comment posted by jayiscoding

is your autocomplete pulling from HTML or JS? I have many languages in the javascript file that I need to pull from in the dropdown (I only listed a few here as a short example)

Comment posted by Addis

from javascript

By