I am using the jQuery autocomplete plugin like below. This works fine, but I'd like to loop through the values of the selected item. For example, if 'Product 1' were selected, I'd like to log this to the console:
label : Product 1
value : value1
tax : 18%
I just cannot figure how to do this. If I do console.log(ui)
I can see the values, but how do I loop through them? Can you please help?
<div class="input-group">
<span class="input-group-addon">choices ...</span>
<input type="text" class="form-control" placeholder="choice" name="choice" id="choice">
</div>
<div class="input-group">
<span class="input-group-addon">value of choices ...</span>
<input type="text" class="form-control" placeholder="" name="prid" id="prid">
</div>
$(function() {
$("#choice").autocomplete({
source: [{
label: "Product 1",
value: "value1",
tax: "18%"
},
// These aren't fixed. Some products have, some less.
{
label: "Product 2",
value: "value2",
tax: "24%"
}
],
minLength: 1,
delay: 0,
select: function(event, ui) {
event.preventDefault();
$('#choice').val(ui.item.label);
this.value = ui.item.label;
$('#prid').val(ui.item.tax);
// I'm stuck over here
for (var i = 0; i < ui.length; i++) {
console.log(ui[i]);
}
}
});
});