I would like to make a form with list of products.
When the user starts typing the product name, autocomplete should show the available options by name (first array element).
When the user chooses a product, the second form control should display a price (second array element).
Here is my code:
HTML:
Product name:
<textarea class="form-control" id="product_name" name="product_name" rows="1" cols="70" placeholder="" type="text" required></textarea>
<br>
Product price:
<textarea class="form-control" id="product_price" name="product_price" rows="1" cols="70" placeholder="" type="text" required>
</textarea>
JS:
$(function() {
var tagi = [
["product_name_1", "10"],
["produc_name_2", "20"],
["produc_name_3", "20"],
];
$( "#product_name, #product_price" ).autocomplete({
source: tagi[0,1]
});
});
...and js fiddle: http://jsfiddle.net/dNLQa/159/
I am trying get this to work but I am stuck.
PS. The array will be generated by PHP, so if this needs to be in a different format I can do that easily.
tagi[0, 1]does not make sense.[ {name: "product_name_1", price: "10"}, {name: "product_name_2", price: "20"}, {name: "product_name_3", price: "30"} ]). jQuery autocomplete supports displaying a certain property from the objects in an array.Array#mapto extract the relevant property, just like Davide's answer shows. jsfiddle.net/dNLQa/166.