What is the proper syntax for using an external JavaScript data source with jQuery UI autocomplete?

Thank you for taking the time to read this. I am working on customizing the jQuery UI autocomplete search feature to display clickable link results, and so far, I have been successful in my efforts by referencing code from another query on this forum.

My challenge now is that I anticipate having a large number of links to display which cannot be efficiently stored as a variable directly on the page. In the past, I used the old version of jQuery autocomplete (pre-UI) and was able to store the variable "updates" in a separate javascript file. However, with the new jQuery UI version, I am unsure how to achieve the same result. The reference link for the previous version can be found here...

The javascript file utilized there was named "localdata.js"

If anyone knows how to transition from sourcing data from a variable on the HTML page to an external javascript file with identical content for the jQuery UI version, your help would be greatly appreciated.

I have also consulted the migration guide available at but have not been able to resolve this issue yet.

The existing working code that does not refer to an external file is provided below...

    <script>
    $(function() {


    var updates = [ 

    { value: "http://www.google.com", label: "Google"},
    { value: "http://www.yahoo.com", label: "Yahoo!"},
    ];

    $("input#autocomplete").autocomplete({
    source: updates,
    select: function( event, ui ) { 
    window.location.href = ui.item.value;
    }
    });
    });
    </script>

I understand that the required change needs to be made on the line "source:updates," but I am unsure of the correct steps. Thank you once again for reading and any assistance you may provide.

Answer №1

To achieve this, consider storing your data in a JSON format within a separate file. For example, convert your updates array into a valid JSON structure:

[{"value": "http://www.google.com", "label": "Google"},
 {"value": "http://www.yahoo.com", "label": "Yahoo!"}]

Make sure to use double quotes around the keys and values without declaring variables, then save the content in a file named updates.json. By doing so, you can set the source attribute in autocomplete to reference updates.json:

$("input#autocomplete").autocomplete({
  source: "updates.json",
  select: function( event, ui ) { 
    window.location.href = ui.item.value;
  }
});

This setup assumes that the file will be served from the same domain. It's also worth noting that autocomplete supports remote domain calls using JSONP.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating the value of a key within a jQuery object

A custom modal plugin has been developed with options that can be set by default or by the user. These values are passed to a function for updating, and it is desired that the options object as a whole is updated rather than individual values. The user&ap ...

Is there a way to filter and tally the JSON objects that have latitude and longitude within a 10km radius from the

I'm currently working on filtering and counting objects from an API endpoint that fall within a 10km radius of the specified origin. My main challenge lies in correctly filtering the API results and tallying the number of matching items. While I succ ...

Elevation in design ui component

I am having an issue with the height of a theme-ui component I embedded. Even though the console shows it correctly, it is displaying at 100% height. <Embed src={url} sx={{width: '800px', height: '400px'}}/> This embed is contain ...

Retrieve worldwide data for the entire application in Next.js during the first page load

Within my Next.js application, I am implementing search filters that consist of checkboxes. To display these checkboxes, I need to retrieve all possible options from the API. Since these filters are utilized on multiple pages, it is important to fetch the ...

gathering identical objects in the collection

I need to calculate the total time of the specified objects and display it in a single list. Here is the object data: var list = [ { 'user': 'Tom', time: 270547 }, { 'user': 'Alex', time: 82081 }, { 'user&apo ...

Why does the custom method only trigger once with the addEventListener?

I am attempting to connect the "oninput" event of an input range element to a custom method defined in a corresponding typescript file. Here is the HTML element: <input type="range" id='motivation-grade' value="3" min="1" max="5"> This i ...

Do not begin the next task until the current function has properly concluded

I am currently developing a project in Ionic v4 with Angular that involves using BLE to communicate with a Raspberry Pi. The project consists of several steps: Searching for devices around me Connecting to the desired device Activating notifications Sen ...

When working with arrays in a programming loop, is it better to assign the value to a variable or access it directly?

When facing a complex for loop with lots of operations, what is the most efficient way to iterate through it? for ($i = 0; count($array) > $i; $i++) { $variable = $array[$i]; $price = $variable->price; OR $price = $array[$i]->price; } T ...

What could be causing the issue with my custom AlloyEditor UI extension?

After following the instructions in this guide to integrate alloyeditor as a WYSIWYG editor into Contentful, I successfully added the extension to my contentful staging space. However, despite copying the html page from the github repository and includin ...

What are the steps for creating personalized sliders with WordPress?

Seeking guidance on how to code WP Template files to enable control from the WP dashboard for adding or removing slider images. A sample code snippet is provided below. <div id="myCarousel" class="carousel slide" data-ride="carousel"> <!-- Indi ...

What is the syntax for creating a zip function in TypeScript?

If I am looking to create a zip function: function zip(arrays){ // assume more than 1 array is given and all arrays // share the same length const len = arrays[0].length; const toReturn = new Array(len); for (let i = 0; i < len; i+ ...

Unable to establish React API communication on cloud-based IDE for MERN Stack development

Exploring the MERN stack through this informative tutorial: https://medium.com/@beaucarnes/learn-the-mern-stack-by-building-an-exercise-tracker-mern-tutorial-59c13c1237a1 I've opted to use goorm IDE, a cloud platform similar to cloud 9 IDE. As I pro ...

Tips for blocking submissions when a user tries to input a hyperlink

I have encountered a problem in my journey of learning JS and unfortunately, I couldn't resolve it by myself. Lately, spam has been flooding through the form on my website and all my attempts with jQuery and JS to fix it have failed. As a last resort ...

Using regular expressions to extract substrings from URL

I have different URL routes, such as var url = '/product' and '/product/create'. Is there a way to use Regex in order to extract only the route name like 'product'? ...

What could be causing the context of 'this' in Javascript to remain unchanged in this particular scenario?

Currently, I am exploring the concept of 'this' in Javascript and have encountered a perplexing situation. After delving into how JavaScript functions operate as outlined here, I grasped that when a function is invoked on an object, the object i ...

What's the best way to incorporate a clear options icon when choosing an option in a MaterialUI select component?

I am looking to enhance the user experience by adding a clear options icon that appears when a selection is made from the list of options. <Select InputProps={{ startAdornment: ( <InputAdornment position='end'> <Cl ...

When a Vue.js datepicker is set as required, it can be submitted even if

When using the vuejs-datepicker, setting the html required attribute on input fields may not work as expected. This can lead to the form being submitted without an input value. <form> <datepicker placeholder="Select Date" required></datep ...

WebRTC error encountered: Unable to add ICE candidate to 'RTCPeerConnection'

Encountering a specific error in the browser console while working on a project involving p2p video chat. The error message is Error: Failed to execute 'addIceCandidate' on 'RTCPeerConnection': The ICE candidate could not be added.. Int ...

Updating form in react requires a double click

I'm facing an issue with the popup form in my note-taking project. The popup form displays when a user wants to edit a note, and it should already contain the contents of the selected note. While the form shows the correct contents, I've noticed ...

Error in TypeScript code for combined Slider and Input onChange functionality within a Material-UI component

HandleChange function is used to update the useState for Material-UI <Slider /> and <Input />. Here is the solution: const handleChange = (event: Event, newValue: number | number[]) => { const inputValue = (event.target as HTMLInputEle ...