Bhushan Dafale Bhushan Dafale - 3 months ago 17
HTML Question

How can i get json key and value from autocomplete response in separate textbox. please see image

enter image description here

Please see images you will understand...this image taken from chrome development tool, i am printing JSON response on console.

Following is autocomplete code...

$("#search-org").autocomplete({
width: 300,
max: 10,
delay: 100,
minLength: 1,
autoFocus: true,
cacheLength: 1,
scroll: true,
highlight: false,
source: function(request, response) {
$.ajax({
url : "../../SearchHelperController",
contentType : "application/json; charset=utf-8",
dataType : 'json',
type : 'GET',
cache : false,
data: {
searchKeyword: request.term,
searchType: $("#byName").val()
},
success: function(data, textStatus, jqXHR) {
console.log(data);
response(data);
},
error: function(jqXHR, textStatus, errorThrown){
console.log( textStatus);
}

});
},
select: function (event, ui) {
$("#org-id").val(ui.item.key);
},

});


html code..

<div class="radio">
<label><input type="radio" id="byName" name="byName" value="OU" checked> By Name</label>
<input type="text" id="org-id">
</div>
<div class="form-group">
<input type="text" class="form-control" id="search-org">
</div>

Answer

to get an object keys, simply write:

var keys = Object.keys(data);

this will give you an array of the keys for this object. to access them use

keys.forEach(function(key) {
   console.log(data[key]);
});
Comments