In a form I have 2 inputs. In the 1st input I use a datalist which I load it through JSON and the 2nd input is autocomplete it when the 1st input is changed.
Till here all works fine!
I added a new button, which I use popover from bootstrap which I want to show some text there depending the selection of the user (as above). The text I want to show is the information of the element labels
.
I read a relevant question but I haven’t succeeded till now..
var dataList = $('.products');
var jsonOptions = [{
"product": "11111",
"description": "description 1",
"labels": [{
"version": "01",
"quantity": 500
}, {
"version": "02",
"quantity": 800
}, ]
}, {
"product": "22222",
"description": "description 2",
"labels": [{
"version": "01",
"quantity": 900
}, {
"version": "02",
"quantity": 100
}, ]
}, {
"product": "33333",
"description": "description 3",
"labels": [{
"version": "01",
"quantity": 200
}, {
"version": "02",
"quantity": 4300
}, ]
}];
jsonOptions.forEach(function(item) {
var option = '<option value="' + item.product + '">' + item.description + '</option>';
dataList.append(option);
});
$(function() {
$('body').on('input', '.product,.products', function() {
var i = this.value;
var description = "";
var productsInBox = 0;
var text = "";
jsonOptions.forEach(function(a) {
if (a.product == i) {
description = a.description;
productsInBox = a.productsInBox;
for (let i = 0, l = a.labels.length; i < l; i++) {
text += "version " + a.labels[i].version + " has " + a.labels[i].quantity + "n"
}
}
});
$(this).closest('.form-group').find('.description').val(description);
$(this).closest('.form-group').find('.mytext').val(text);
console.log(text);
});
});
$('[data-toggle="popover"]').popover();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<form id="form1" method="post" class="form-horizontal" role="form">
<fieldset>
<div class="form-group">
<div class="col-sm-2">
<input type="text" list="products" class="form-control product" name="product[]" />
<datalist id="products" class="products"></datalist>
</div>
<div class="col-sm-3">
<input id="" type="text" class="form-control description" name=" description[]" />
</div>
<div class="col-sm-3" style="margin-left: 10px;">
<button type="button" class="btn btn-info btn-group-sm mytext" data-toggle="popover" data-content="text to change to like: version 01 has 500 "><i class="fa fa-info"></i></button>
</div>
</div>
</fieldset>
</form>
2
Answers
You can use another
forEach
loop to iterate over thelabels
and get theversions
andquantities
for the current selection, append them to a predefined empty variable and then add them to the popover usingattr()
method:If I understand you correctly, you will need to update attribute data-content based on selected value.
Updated your snippet.
Probably you will need to handle: how to close popover when selection is changed