Autocomplete input text HTML tag using dynamic history data from json that is loaded in vanilla javascript using datalist tag?

I have a JSON file

names.json

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"]
</code>
<code>["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"] </code>
["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"]

Now this file is a few MiB large which has a hundred thousand names. This file is exported as JSON serialized format from a database which has a history of names typed into the input box.

Here is a simple HTML page to replicate an input

index.html

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!DOCTYPE HTML>
<HTML>
<head>
</head>
<body>
<div>
Enter Name:
</div>
<div>
<input type="text" />
</div>
</body>
</HTML>
</code>
<code><!DOCTYPE HTML> <HTML> <head> </head> <body> <div> Enter Name: </div> <div> <input type="text" /> </div> </body> </HTML> </code>
<!DOCTYPE HTML>
<HTML>
<head>
</head>
<body>

<div>
Enter Name:
</div>

<div>
<input type="text" />
</div>

</body>
</HTML>

This page is new, it does not have any history of the input tags nor will it submit any form to create new history, I only want to load the data I provide it.

How would I go about loading the autocomplete suggestions? Can someone please suggest?

Something that works which looks like –

Desired output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>globalThis.namelist = JSON.parse("names.json");
console.log(globalThis.namelist.length);
<input type="text" list="globalThis.namelist" />
</code>
<code>globalThis.namelist = JSON.parse("names.json"); console.log(globalThis.namelist.length); <input type="text" list="globalThis.namelist" /> </code>
globalThis.namelist = JSON.parse("names.json");
console.log(globalThis.namelist.length);
<input type="text" list="globalThis.namelist" /> 

And does not take too many lines of code to solve a missing attribute for datalist tag support for JS lists. Hard Coding the options without reading the JSON is not allowed or I would have done it. Thank you.

  1. Load the JSON file with a script tag to deserialize(a step in unmarshalling) the data.

<script src="names.json"></script>

  1. Then populate a datalist tag with option tags using vanilla JS.

<datalist id="name-suggest"></datalist>

Should work with a few MiB of data to autocomplete in real time.

Here are the updated files

names.json

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>globalThis.namelist = ["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"]
</code>
<code>globalThis.namelist = ["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"] </code>
globalThis.namelist = ["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"]

Update the names.json file to store it’s value in browser variable namelist.

HTML file now has datalist tag to be populated.

It should also load the names.json as script and save it in globalThis.namelist.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>window.onload = function() {
globalThis.myenv = {};
globalThis.namelist = ["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"]
// above line is substitute for script tags.
globalThis.myenv.addToDatalist = function(datalistID, datalist) {
let suggest = "";
for (name of datalist) {
suggest = suggest + "<option value ="" + name + ""></option>";
}
document.getElementById(datalistID).innerHTML = suggest;
};
globalThis.myenv.postload = function() {
console.log(globalThis.namelist.length);
globalThis.myenv.addToDatalist("name-suggest", globalThis.namelist);
console.log("ready!");
};
globalThis.myenv.postload();
}</code>
<code>window.onload = function() { globalThis.myenv = {}; globalThis.namelist = ["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"] // above line is substitute for script tags. globalThis.myenv.addToDatalist = function(datalistID, datalist) { let suggest = ""; for (name of datalist) { suggest = suggest + "<option value ="" + name + ""></option>"; } document.getElementById(datalistID).innerHTML = suggest; }; globalThis.myenv.postload = function() { console.log(globalThis.namelist.length); globalThis.myenv.addToDatalist("name-suggest", globalThis.namelist); console.log("ready!"); }; globalThis.myenv.postload(); }</code>
window.onload = function() {

globalThis.myenv = {};

globalThis.namelist = ["Ant Man", "Aquaman", "Iron Man", "John Wick", "Superman"]
// above line is substitute for script tags.

globalThis.myenv.addToDatalist = function(datalistID, datalist) {
let suggest = "";
for (name of datalist) {
suggest = suggest + "<option value ="" + name + ""></option>";
}
document.getElementById(datalistID).innerHTML = suggest;
};

globalThis.myenv.postload = function() {
console.log(globalThis.namelist.length);
globalThis.myenv.addToDatalist("name-suggest", globalThis.namelist);
console.log("ready!");
};

globalThis.myenv.postload();

}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!DOCTYPE HTML>
<HTML>
<head>
</head>
<body>
<div>
Enter Name:
</div>
<div>
<input type="text" list="name-suggest"/>
</div>
<datalist id="name-suggest">
</datalist>
<!--
<script src="names.json"></script>
<script src="index.js"></script>
I am commenting this line because code snippet will not run it.
Replace the line in js with script tag commented here.
-->
</body>
</HTML></code>
<code><!DOCTYPE HTML> <HTML> <head> </head> <body> <div> Enter Name: </div> <div> <input type="text" list="name-suggest"/> </div> <datalist id="name-suggest"> </datalist> <!-- <script src="names.json"></script> <script src="index.js"></script> I am commenting this line because code snippet will not run it. Replace the line in js with script tag commented here. --> </body> </HTML></code>
<!DOCTYPE HTML>
<HTML>
<head>
</head>
<body>

<div>
Enter Name:
</div>

<div>
<input type="text" list="name-suggest"/>
</div>

<datalist id="name-suggest">
</datalist>

<!--
<script src="names.json"></script>
<script src="index.js"></script>
I am commenting this line because code snippet will not run it.
Replace the line in js with script tag commented here.
-->
</body>
</HTML>

Now you can type in the text box and the names should be suggested in real time.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật