Issues with Materialize autocomplete drop-down functionality

I've been working on implementing materialize autocomplete in my project and have been facing issues with the dropdown not appearing. I tried using materialize version 0.98.2 from CDN as suggested, but it still doesn't seem to work. I suspect there might be a problem with triggering the dropdown, or perhaps an issue with the data not being utilized correctly.

While I am able to retrieve and use the data without any problems, the dropdown functionality simply won't work.

Here's the HTML:

<div class="input-field">
    <label for="country">Autocomplete</label>
    <input type="text" id="company" class="autocomplete">
</div>

And here is my JavaScript code:

var company = document.getElementById("company");

  company.addEventListener("input", function () {
      $(function() {
      var inputValue = $('#company').val();
      var url = 'https://sandbox.iexapis.com/stable/search/'
              + encodeURIComponent(inputValue)
              + '/?token=****************';
      
       $.ajax({
        type: 'GET',
        url: url,
        success: function(response) {
          
          var companyArray = response;
          var dataCompany = {};
          for (var i = 0; i < companyArray.length; i++) {
            //console.log(countryArray[i].name);
            console.log(url);
            dataCompany[companyArray[i].securityName] = companyArray[i].symbol;
            console.log(companyArray[i].securityName);
            console.log(companyArray[i].symbol);
          }
          $('input.autocomplete').autocomplete({
            data: dataCompany
          });
        }
      });
    });
  });

As requested, here are some screenshots of what happens when I enter "wa":

https://i.sstatic.net/QzydI.png

The issue seems to be that while the data is present, the dropdown selector box fails to appear.

Answer №1

To begin, initialize the autocomplete feature and save an instance of it in a variable. Then, as the user types in the input box, utilize this instance to update the data in your autocomplete like so: instance.updateData(dataCompany).

Sample Code :

//assume this is the response received from an AJAX request
var response = [{
  "securityName": "abc",
  "symbol": "scsc"
}, {
  "securityName": "abc2",
  "symbol": "scsc2"
}]

$(function() {
  $('input#company').autocomplete() //initialize the autocomplete
  let instance = M.Autocomplete.getInstance($('input#company')); //get the instance
  $('input.autocomplete').on("input", function() {
    var inputValue = $('#company').val();
    /*var url = 'https://sandbox.iexapis.com/stable/search/' +
      encodeURIComponent(inputValue) +
      '/?token=****************';
    $.ajax({
      type: 'GET',
      url: url,
      success: function(response) {*/
    var companyArray = response;
    var dataCompany = {};
    for (var i = 0; i < companyArray.length; i++) {
      dataCompany[companyArray[i].securityName] = companyArray[i].symbol;

    }
    //update data
    instance.updateData(dataCompany)
    /*  }
    });*/

  });
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>


<div class="input-field">
  <label for="country">Autocomplete</label>
  <input type="text" id= "company" class="autocomplete" autocomplete="off">
</div>

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

What is the best way to customize the appearance of an XML snippet using Greasemonkey?

I am attempting to customize an XML fragment retrieved from a server response using a Greasemonkey script. Take a look at this sample XML fragment from w3schools.com: <note> <to> Tove</to> <from>Jani</from> <heading ...

Instruct Smarty to display the block exactly as it is

Struggling to inline some JavaScript code into the Smarty template files due to the {ldelim} {rdelim} markers. Is there a way to instruct Smarty to disregard the markup and just output it as is? Any similar approach like CDATA blocks in XML? Just demonstr ...

Best practice for finding the parent element using Protractor

The recently released Guidelines advise against using the by.xpath() locators. I am making an effort to adhere to this suggestion, but I'm having difficulty locating a parent element. We are currently utilizing the .. XPath expression to access the p ...

Encountering a problem when trying to dynamically change the value in a dropdown menu with Angular.js

Having an issue where two drop down lists have the same value. When I set a value to the first drop down list, the second one also takes that value. Below is my code for reference: <div class="col-md-6"> <div class="input-group bmargindiv1 col-md ...

Initiate a Gravity Forms form refresh after modifying a hidden field with jQuery

TO SUM IT UP: Is there a way in Javascript to activate an update on a Gravity Form that triggers the execution of conditional logic? ORIGINAL QUESTION: I'm using Gravity Forms and I have set up an "on change" event $('#gform_1').find(&apos ...

What could be the reason behind ng-bind-html only displaying text and not the link?

Utilizing ng-repeat to exhibit a list on my webpage. One of the fields in my data contains a URL that I want to display as an actual link within my HTML page. Please refer to the screenshots below: My HTML: https://i.sstatic.net/2Gj1U.png My rendered pa ...

show a pie chart at the top of the div using highcharts

i am struggling with positioning the high-chart with a label, as seen in the image below https://i.sstatic.net/m9bXW.png does anyone know how to properly display the chart from the top left corner? i have attempted to adjust the settings like this { ...

How to Retrieve Grandparent Component Attributes in Angular Using Grandchild Components

I am constructing an Angular application and facing the challenge of accessing a property of Component 1 within Component 3. In this scenario, the relationship is described as grandparent-grandchild. Successfully establishing communication between parent/ ...

I am looking to modify a particular value within an array of objects, but for some reason, the update is not being applied correctly

When attempting to copy the array, I update the selected value of a specific object based on the matching memberId. This process works well for a single member, however, issues arise when there are multiple members as the updating doesn't work correct ...

Issue with highcharts and vue.js: creating a unique chart for displaying measurements

Currently, I am integrating Highcharts and Vue.js simultaneously while incorporating multiple charts on my webpage. As of now, I have successfully displayed data on all the charts without encountering any issues. However, my goal is to assign a special tit ...

What could be causing the NoScript tag to malfunction across different web browsers?

I've incorporated the NoScript tag into my JSP pages in the head section. To avoid conflicts with tiles, I made sure not to include multiple NoScript tags. However, I am experiencing issues in Internet Explorer where it doesn't seem to be working ...

What is the best way to create a unit test for a function that calls upon two separate functions?

When testing the getAppts function within this module, the correct way to evaluate the code it encompasses may not be entirely clear. Should db.getDatabase() and fetchAppts() be run as stubs inside the unit test function? The current unit test implementati ...

What is the best way to show only the weekdays on the x-axis?

My goal is to create a scatter-linked graph using D3.js, showcasing the people count for various shifts on different dates in January 2020. Here is the code snippet I am working with: <!DOCTYPE html> <html lang="en" > <head> & ...

Utilizing Laravel 8 for seamless file uploads with AJAX on onchange event

I'm trying to implement a feature in my Laravel 8 application where users can upload multiple files using the onchange event. Is it possible to achieve this functionality with just the onchange event? Below is the HTML form I currently have. Any assis ...

Need assistance with jQuery AJAX?

Hey there, I'm a new member and I've gone through the different Ajax Help topics but still can't figure out why my code isn't working. Here's what I have: $(document).ready(function(){ $.ajax({ type: "GET", ur ...

Utilizing API data as props within an Autocomplete component in Reactjs with Material-UI

New to Reactjs, I am currently exploring the implementation of the Autocomplete component from material-ui. My goal is to pass the API link as a prop to the element. However, I'm stuck on how to pass the json label name as a prop to be used in "getOpt ...

Trouble with triggering the fn.yiiGridView.update event

I am new to the Yii framework and need assistance with implementing a filter functionality for a CGridView based on a dropdown selection. I have gone through several tutorials, but none of them seem to work as expected. One such tutorial I referred to can ...

custom popup click event in angular leaflet directive does not work as expected on iOS devices

For the development of a map on iOS devices, I am utilizing the angular leaflet directive and ionic. I am facing an issue where the popup is not clickable on the iOS simulator, even though it works fine on web browsers. var html11 = '< ...

Avoiding remote submission in Grails forms: Best practices

<g:formRemote name="form1" update="homeBody" url="[controller: 'xxx', action:'aaa']"> <Button type="submit">Save</Button> </g:formRemote> I am facing a scenario where I need to place a text field o ...

Steps for coloring an image using pixel values retrieved from a database

My goal is to create a heat map by dividing an image I captured from Google Maps into a grid, which can be seen here: https://i.sstatic.net/uNv4e.jpg I want to assign colors to specific grid squares based on the number of people living in them. For exampl ...