Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Autocomplete - Remote JSONP datasource</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
    <h2>Operating Systems</h2><br />
    <label for="select">Choose: </label>
    <input type="text" id="select" />
    <label id="selectID"></label>
    <script>
        (function () {
            var url = "http://localhost:61397/CompanyRestService.svc/GetCompID/0";
            var items = [];          
            $.getJSON(url)
               .done(function (data) {
                   $.each(data, function (i, item) {
                       items[i] = item.Name;
                   });
               });
            $('#select').autocomplete({ source: items});
        })();
    </script>
    <script>
        function myFunction() {
            var x = document.getElementById("select").value;
            document.getElementById("demo").innerHTML = x;
        }
    </script>
    <button onclick="myFunction()">Click me</button>
    <p id="demo"></p>
</body>
</html>

HTML



What I have tried:

my Json output:
[{"Id":155,"Name":"DOMEX E-DATA PRIVATE"},{"Id":156,"Name":"HCL COAL INTERNATIONAL PRIVATE LIMITED"},{"Id":158,"Name":"Roja Builders and services"},{"Id":159,"Name":"TATA ADVANCED MATERIALS LIMITED"},{"Id":161,"Name":" Tikona BroadBand ltd"},{"Id":166,"Name":"DOMEX TECHNICAL INFORMATION PRIVATE LIMITED"},{"Id":168,"Name":"Philips India"}]

I m try ind the above code the successfully work ...but i dont know ,how to get "Id " ....plz help...How to get autocomplete selected "Id" to any lable or hidden field...
Posted
Updated 23-Jun-16 20:26pm

1 solution

try this

JavaScript
(function () {
var url = "http://localhost:61397/CompanyRestService.svc/GetCompID/0";
var items = [];
$.getJSON(url)
.done(function (data) {
$.each(data, function (i, item) {
items[i] = item.Name;
});

$('#select').autocomplete( {
source: items,
select: function (event, ui) {
var value = ui.item.value;
var id = '';
for (var i = 0; i < data.length; i++) {
if (value == data[i].Name) {
id = data[i].Id;
break;
}
}
$("#selectID").html(id);
return false;
}
}
);

});

})();
 
Share this answer
 
v2
Comments
venkatesh (chennai) 24-Jun-16 2:54am    
they come only value.. not getting "Id"..
Karthik_Mahalingam 24-Jun-16 3:06am    
check the updated solution.
venkatesh (chennai) 25-Jun-16 0:34am    
thank u sir....work aaguthu sir...
venkatesh (chennai) 25-Jun-16 0:36am    
na yapai sir jquery la master aagurathu...plz tell to advice sir
Karthik_Mahalingam 25-Jun-16 0:39am    
Cool

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900