Failure encountered when attempting to load JSON data into datalist

I've been trying to incorporate inputs into a datalist in two different ways. However, I've encountered an issue with the first method not working properly. --> Check it out on bootlply

var dataList = document.getElementById('json-datalist');

var jsonOptions = [{
                    "product": "11111",
                    "description": "description 1"
                  }, {
                    "product": "22222",
                    "description": "description 2"
                  }, {
                    "product": "33333",
                    "description": "description 3"
                  }, {
                    "product": "44444",
                    "description": "description 4"
                  }, {
                    "product": "55555",
                    "description": "description 5"
                  }]; 

jsonOptions.forEach(function(item) {

  var option = document.createElement('option');

  option.value = item.description;
  option.text = item.product;
  option.setAttribute('data-product', item.product);
  
  dataList.appendChild(option);
});

Then, include the following:

<div class="container">

    <div class="form-group">
         <div class="col-xs-4">
           <input class="form-control" name="description" type="text" id="ajax" list="json-datalist">
           <datalist id="json-datalist"></datalist>
         </div>


          <div class="col-xs-2">
            <datalist id="material">
              <option value="1">
              </option><option value="2">
              </option><option value="3">
            </option></datalist>
            <input type="text" list="material" class="form-control" name="material-0" placeholder="material">
          </div>

     </div>   

</div>

Answer №1

An additional , is leading to a syntax error in the code snippet.

  option.setAttribute('data-product', item.product);
  , // <-------------------------------------------- This tiny bug needs fixing
  dataList.appendChild(option);

All you need to do is remove it and everything will be back on track. :)

  option.setAttribute('data-product', item.product);
  dataList.appendChild(option);

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

Control the data options available in the autosuggest textbox

I have implemented an autosuggest feature in a text box with predefined values. When typing the first letter in the textbox, a dropdown displays all related values. Is there a way to limit the number of displayed related values to 20? This is what my cu ...

What are the best methods for implementing runtime type checking in JavaScript?

Utilizing either TypeScript or Facebook's Flow (type), I am empowered to statically assign types to variables like this: function add (x: integer, y: integer) { ... } Both TypeScript and Flow are able to identify and prevent incorrect invocations su ...

Angular page not reflecting show/hide changes from checkbox

When the user clicks on the checkbox, I need to hide certain contents. Below is the code snippet: <input id="IsBlock" class="e-field e-input" type="checkbox" name="IsBlock" style="width: 100%" #check> To hide content based on the checkbo ...

Using PHP to retrieve JSON data from OpenStreetMap search

I'm attempting to utilize Openstreetmap and PHP to pinpoint a location on a map. Below you can see that I am fetching a JSON array, however, PHP is returning a NULL value. Please note that my $url contains valid JSON (you can validate it here). < ...

What is the best way to retrieve the value from a textbox on the client side and then utilize it to generate

I am currently utilizing jQuery for my modal dialogs. My goal is to open a model dialog from one page and send some additional query strings to the modal dialog page. Is there a way to achieve something like this? <asp:HyperLink ID="hypClientSearch" ru ...

The Monaca API for managing push notifications is currently experiencing technical issues

Attempting to follow the Monaca Push Notification API tutorial () in order to send a push notification to my iPhone through curl from the command prompt. When using the following command: curl --insecure https://cloud.monaca.mobi/manage/json-rpc/##### -- ...

Assigning a variable in jQuery to a PHP variable that has not been defined can halt the script

Here is the code snippet for a document ready function: $(document).ready(function(){ var id = <?php echo "" ?>; alert('boo'); if(id!=0){ $(' ...

Uncover the Layers of JSON Data with MiniJSON in Unity3D

As a beginner with JSON, I am struggling with nested JSON structures. After searching for two days without success, I have come across various examples of how to deserialize nested JSON but none of them seem to work for me. My main query is regarding the ...

Why does jQuery's each function struggle to retrieve the width of hidden elements?

Why is it difficult to obtain the width of hidden elements? Here is my code: .hidden { display: none; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <ul class="navbar-items"> <li> ...

Exploring the functionality of inline easing when using the ScrollTo plug-in

Attempting to utilize the ScrollTo plug-in with an easing effect. I prefer not to include the easing plug-in file, as I will only use it once and for a single easing effect. The specific 'easeOutBack' function I aim to implement is: easeOutBac ...

Reverting Changes Made with JQuery Append

I have a table where each cell contains a button. When the button is pressed, it adds text to the cell. I am looking for a way to remove this text from the cell when the same button is pressed again. It's important to note that this button appears mul ...

The longevity of JQuery features

As I work on setting up an on-click callback for an HTML element to make another node visible, I encountered a surprising realization. The following two statements appeared to be equivalent at first glance: $("#title").click($("#content").toggle); $("#tit ...

Error has occurred: Unable to assign a value to an undefined property using axios

Within my Vue.js component, I am utilizing axios to fetch a JSON array consisting of `joke` objects: <template> <div id="show-jokes-all"> <h2>Showing all jokes</h2> <div v-for="joke in jokes"> ...

Glitches and sudden jumps occur when using the useMediaQuery function in Material UI

Implementing Material UI's useMediaQuery() hook, I have embedded the theme provider and initialized a variable in this way: const theme = useTheme(); const isSmall = useMediaQuery(theme.breakpoints.down('sm') ); I am utilizing the isSmall v ...

Tips on keeping a div element in a fixed position until triggered by jQuery

I've managed to create a navigation bar within a header that sticks to the top of the screen once you scroll down past 100px. The functionality works well, but I'm looking to make the navigation bar stay fixed on the right until it snaps, essenti ...

Assistance needed with securely reading login information from text file using PHP

Here is a snippet of code related to saving registration data to a text file. Now, on the login page, I need to read and verify this data so that users can log in. <?php if (isset($_POST['submit'])) { $name = $_POST['forename&apo ...

Restrict the query based on a value within a nested JSON array

Creating a basic table in PostgreSQL 12.3 create table aaa(id integer, data json); The data column is used to store JSON data with the following structure: {     "foo": 1,     "persons": [         {   ...

Is there a way for me to include .js or .cpp prior to using the Vim shortcut key gf?

When using a programming language like nodejs, the require function does not require the addition of the .js extension. For example: var Assert = require('./app_base'); However, in vim, when using gf, it cannot find app_base without the .js ext ...

What is the best way to verify multiple email addresses simultaneously?

Is there a method to validate multiple email addresses entered by users in a textarea? My current approach involves using ng-pattern for validation. ...

Creating randomized sequences using JavaScript

One of my hobbies involves organizing an online ice hockey game league where teams from different conferences compete. It's important to me that every team gets an equal number of home and away matches throughout the season. To simplify this task, I&a ...