The search function in Select2 is not displaying the desired result

I'm encountering an issue with the search functionality when it uses Ajax to load data from a JSON file. For example, if I search for Yemen, the record for Yemen does not get selected or highlighted properly.

Here is the complete source code - could there be a misconfiguration somewhere?

<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" ></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a1acacb7b0b7b1a2b883edf3fadce9eee2">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

<script>
jQuery(document).ready(function() {

    $('#allowedCountries').select2({
        placeholder: 'Select allowed countries',
        allowClear:true, 
        tokenSeparators: [',', ' '],
        ajax: {
            dataType : "json",
            url      : "countries2.json",
        },
    });    
});
</script>
</head>

<body>
for select2<Br>

<div style="width:100%">
    <select name="allowedCountries" class="js-states form-control" id="allowedCountries"><option></option></select>
</div>
    
</body>
</html>

And here is a sample of the JSON file used as the data source:

{
    "results": [
    {
        "id": 1,
        "text": "Afghanistan"
    },
    {
        "id": 2,
        "text": "Aland Islands"
    },
    {
        "id": 3,
        "text": "Albania"
    },
    {
        "id": 245,
        "text": "Yemen"
    },
    {
        "id": 246,
        "text": "Zambia"
    },
    {
        "id": 247,
        "text": "Zimbabwe"
    }
    ]
}

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

When the table is clicked, a dynamic accordion table should appear

My current code displays a table inside another table using a PHP for loop. While I can retrieve the data successfully, I'm facing issues with the UI layout and accordion functionality. The inner table is displaying beside the outer table instead of b ...

How can I retrieve the $index value of an ng-repeat element within a uib-dropdown?

I am currently working on implementing an ng-repeat loop that includes a dropdown menu for each element. I want the dropdown menu to contain functions that operate on the specific element, requiring access to the index of that element. Below is the code sn ...

Retrieve the latency of the interaction.reply() method

While there have been many inquiries regarding how to create a ping command for a discord.js bot, my question stands out because I am attempting to develop this command for interaction rather than message. I attempted utilizing Date.now() - interaction.cre ...

Save a PHP variable and then use it on an HTML page

Is there a way to preserve the value of LAST_INSERT_ID(), also known as Case_ID, so that it can be accessed in another HTML page? If so, what would be the best approach to achieve this? $query.= "insert into Picture (Case_Pic,Case_ID) values ...

Transform the value of a string JSON property into a custom object

I'm working with JSON data that I need to deserialize into a C# object. Here is the structure of the data: { "version": 0, "guid": "2166a7d5744d47009adaa29f0e549696", "csv": "one:1,two:2,three: ...

What is the best way to send a "string request" via POST and retrieve the data?

I recently attempted an application on Android version 23 and encountered a roadblock. I have a special request, but it is not in URL format! It is in String format. I need to post it to the server and retrieve some data. For example, my URL looks like t ...

Transmit the document to Django using Ajax

I am encountering an issue when trying to send an image using an AJAX request to Django. Below is the HTML code I am using: <form> <input type="file" id="files" name="image"> </form> Next, here is the corresponding JavaScript code: var ...

Issues with the Node Package Manager's watch command functionality

Having installed the frontend dependency in my Vue.js project, I attempted to run the command npm run watch. I updated the scripts section in package.json as shown below- "scripts": { "dev": "npm run development", &qu ...

Sharing information among v-for divisions - Vue.js

I am currently delving into the world of VueJS. While my code may not be the most elegant, it does the job almost as intended. The issue I am facing is that the API provides the title and href separately in different v-for loops, even though each loop only ...

Tips for refreshing a DOM element after inserting with jQuery

I'm trying to update the LI element after using the insertBefore function in jQuery. The issue arises after adding new elements to UL where I encounter difficulty deleting the same element (using emailTagRemove). Check out the demo to see the proble ...

Executing a Shortcode Using a Button in Visual Composer for Wordpress

It should be easy to do this. I've got a great plugin with a modal newsletter signup form that offers various launch options, including manual launching with the following codes. https://i.stack.imgur.com/IGbsp.png My theme utilizes Visual Composer. ...

Sending data from the View to the Controller using AJAX in Laravel 5

Data I have some data stored in a variable var info = []; info['upnp_enabled'] = $("#upnp-switch").val(); When I console.log(info);, the output shows [upnp_enabled: "true"] Ajax I attempted to send a PUT request to my Cont ...

tips for transmitting JSON data via the PHP API

I have successfully sent a text push from the PHP API code, but I am now wondering how to send JSON data as well using the PHP API. The issue I'm facing is that when I send JSON from the PHP API code, it is received as a "message" type. Interestingl ...

VueJS: interactive input field with dynamic value binding using v-model

I am facing an issue with VueJS regarding setting the value of an input radio along with v-model. I am confused as to why I am unable to dynamically set a value to an input and use a model to retrieve the user's selection. Here is a clearer represent ...

Enhancing Website Interactivity with PHP, AJAX, and

I recently followed a tutorial on handling AJAX requests with PHP and MySQL, which can be found here. My goal is to update the SQL query based on the value selected from a dropdown menu using the onchange event. function myfunctionTime(time) { if (t ...

Looking to create a clone of an HTML element, make some modifications, and extract the HTML string without impacting the DOM?

After working with some HTML code, I encountered the following: <div style="background:red;width:900px;height:200px" id="wrap"> <div id="inner" style="width:300px;float:left"> ... </div> </div> The tasks at hand are ...

Encountering issues launching cmd.exe using ProcessStartInfo

Web.config: <appSettings> <add key="MystemDirectory" value="D:\mystem\"/> </appSettings> Controller: if (flag) { db.FbDocuments.Add(fbDocument); db.SaveChanges(); var workingPath = WebConfigurationManager.Ap ...

Preventing the page from scrolling to the top while utilizing an Angular-bootstrap modal and a window position:fixed workaround on an iPad

It's common knowledge that there is a bug in bootstrap modals on certain devices, causing the page behind the modal to scroll instead of the modal itself (http://getbootstrap.com/getting-started/#support-fixed-position-keyboards) An easy fix for this ...

Tips for showing a single table with buttons on display

I am using R markdown and have included multiple tables with buttons, but when I open the file, all tables are displayed at once. How can I ensure only one table is shown upon opening the file? https://i.sstatic.net/NFidf.png <script type="text/ja ...

Avoiding duplication of prints in EJS template files

In my EJS code, I have created a loop to fetch the total amount of items from the database. Here is my current code: <h2>Summary</h2> <% if(typeof items.cart!=="undefined"){ var amount = 0; %> <% i ...