yaylitzis
yaylitzis

Reputation: 5544

How to select datalist's description

I have 2 inputs in a form. In the 1st input, I load a datalist through JSON. The json list has 2 attributes machine and lot.

What I want is when the user select a machine, the 2nd input to be filled automatically with the lot. For example If I select 1 the second input to be filled with lot1.

An another approach is to select data-description element. But neither I don't know how..

var dataList = document.getElementById('lots');

var jsonOptions = [{
  "machine": 1,
  "lot": "lot1"
}, {
  "machine": 2,
  "lot": "lot2"
}];

// Loop over the JSON array.
jsonOptions.forEach(function(item) {
  var option = document.createElement('option');
  
  option.value = item.machine;
  option.text = item.lot;
  option.setAttribute('data-description', item.lot);
  dataList.appendChild(option);
});

$(function() {
  $('#machine').on('change keyup', function() {
     var i = this.value;
     
     $('#lot').val(i);
   

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="form-group">
  <div class="col-md-5">
    <input type="text" id="machine" list="lots" class="form-control" name="machine" />
    <datalist id="lots"></datalist>
  </div>
</div>

<br/>

<div class="form-group">
  <div class="col-md-5">
    <input type="text" id="lot" class="form-control" name="lot" />
  </div>
</div>

The actual code I use to load JSON is this:

var dataList = document.getElementById('lots');
var input = document.getElementById('machine');
var request = new XMLHttpRequest();

request.onreadystatechange = function(response) {
  if (request.readyState === 4) {
    if (request.status === 200) {
      // Parse the JSON
      var jsonOptions = JSON.parse(request.responseText);

      // Loop over the JSON array.
      jsonOptions.forEach(function(item) {
        var option = document.createElement('option');

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

      // Update the placeholder text.
      input.placeholder = "machine";
    } else {
      // An error occured :(
      input.placeholder = "error:(";
    }
  }
}; 

// Update the placeholder text.
input.placeholder = "Loading options...";

// Set up and make the request.
request.open('GET', 'lots.json', true);
request.send();

Upvotes: 2

Views: 416

Answers (2)

Sunil B N
Sunil B N

Reputation: 4225

$(function() {
  $('#machine').on('change keyup', function() {
     var i = this.value;
    var value ="";
//fetch from the existing options
     jsonOptions.forEach(function(a){
       if(a.machine == i){
         value = a.lot;
       }
     });
     $('#lot').val(value);
  });
});

Upvotes: 1

Pranesh Ravi
Pranesh Ravi

Reputation: 19113

You need parse through the JSON again to get the lot. See the following code.

And you don't need to set the lot as a data attribute.

var dataList = document.getElementById('lots');

var jsonOptions = [{
  "machine": 1,
  "lot": "lot1"
}, {
  "machine": 2,
  "lot": "lot2"
}];

// Loop over the JSON array.
jsonOptions.forEach(function(item) {
  var option = document.createElement('option');
  
  option.value = item.machine;
  option.text = item.lot;
  dataList.appendChild(option);
});

$(function() {
  $('#machine').on('change keyup', function() {
     var i = this.value;
    var obj = jsonOptions.filter(function(obj){ return obj.machine === parseInt(i)})
     $('#lot').val(obj[0] ? obj[0].lot : '');
   

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="form-group">
  <div class="col-md-5">
    <input type="text" id="machine" list="lots" class="form-control" name="machine" />
    <datalist id="lots"></datalist>
  </div>
</div>

<br/>

<div class="form-group">
  <div class="col-md-5">
    <input type="text" id="lot" class="form-control" name="lot" />
  </div>
</div>

Upvotes: 1

Related Questions