Imagine a json file with the following data:
[
{
color: "red",
value: "#f00"
},
{
color: "green",
value: "#0f0"
},
{
color: "blue",
value: "#00f"
},
{
color: "cyan",
value: "#0ff"
}
]
Using jQuery's autocomplete method, I want it to be able to display color as parameters for selecting and pasting an input value.
<input type="text" name="selector" id="selector" />
<input type="text" name="color" id="color" />
<input type="text" name="value" id="value" />
The above does not require an introduction. A selector for finding colors, input.colorwith a color value and input.valuewith a value.
EDIT:
I have JSON data:
[{
"label": "Sec\u00e7\u00e3o 1",
"value": "1"
}, {
"label": "Sec\u00e7\u00e3o 2",
"value": "2"
}, {
"label": "Sec\u00e7\u00e3o 3",
"value": "3"
}, {
"label": "Sec\u00e7\u00e3o 4",
"value": "4"
}]
and this HTML:
<input type="text" id="name" />
<input type="text" id="value" />
and this jQuery:
$(document).ready(function(){
$("#name").autocomplete({
source: "json.php",
select: function (event, ui) {
$("#name").val(ui.label);
$("#value").val(ui.value);
}
});
});
I followed Andrew's answer and he offers me to select the data, but if I warn the variables ui.labeland ui.value, he says "undefined". What am I missing?
Edit2:
Let's say I have this HTML:
<input type="text" class="name" />
<input type="text" class="value" />
<input type="text" class="name" />
<input type="text" class="value" />
.autocomplete()? , , input.name, input.value ?
[input.name] [input.value]
^ ^
[input.name] [input.value]
^ ^