I am trying to write a custom autocomplete piece for a form, where once you start typing, the suggestions show under the input field. as you type the options get more and more specific. The options are being fed by a JSON array, dynamically created by a mysql query to the database.
Upon clicking on one of the listed options, the click action should fill the input value with the value of the clicked element on the list.
For some reason, I seem to have an issue with the click jquery action on the list of options.
Can someone please look at this source code and help me figure out why is it that it is not working?
I created simplified version of this on jsfiddle.net and it worked, once I put my full code in there it breaks!.
+++++++++++++++++++++++++++
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var partsArray = [
{"label": "0030200-08"},
{"label": "005-2060-6"},
{"label": "005-2063-5"},
{"label": "005-2067-1"},
{"label": "005-2078-3"},
{"label": "005-2119-8"},
{"label": "0062260-16"},
{"label": "007-0026-0"},
{"label": "007-0028-0"},
{"label": "007-0030-0"},
{"label": "007-0031-0"},
{"label": "007-0032-0"},
{"label": "007-0033-0"},
{"label": "007-0035-0"},
{"label": "007-0036-0"},
{"label": "007-0038-0"},
{"label": "007-0039-0"},
{"label": "007-0039-0"},
{"label": "007-0050-0"},
{"label": "007-0051-0"},
{"label": "012-5060-0"},
{"label": "017-0035-0"},
{"label": "017-0041-0"},
{"label": "017-0042-0"},
{"label": "017-0074-0"},
{"label": "019-2055-1"},
{"label": "019-2084-3"},
{"label": "019-2084-6"},
{"label": "019-2099-0"},
{"label": "019-2102-0"},
{"label": "019-2129-2"},
{"label": "019-2151-0"},
{"label": "019-2239-0"},
{"label": "019-2306-0"},
{"label": "019-2313-6"},
{"label": "019-2539-0"},
{"label": "019-3124-0"},
{"label": "019-5065-0"},
{"label": "019-6006-0"},
{"label": "019-6019-0"},
{"label": "019-7042-0"},
{"label": "019-7072-0"},
{"label": "019-7089-0"},
{"label": "019-8040-0"},
{"label": "019-8040-0"},
{"label": "019-8040-0"},
{"label": "019-8040-0"},
{"label": "019-8040-0"},
{"label": "019-8040-0"},
{"label": "019-8046-0"},
{"label": "019-8072-0"},
{"label": "019-8073-0"},
{"label": "019-8074-0"},
{"label": "019-8078-0"},
{"label": "019-8093-0"},
{"label": "019-8094-0"},
{"label": "019-8095-0"},
{"label": "019-8101-0"},
{"label": "023-0115-0"},
{"label": "023-0122-0"},
{"label": "026-0018-0"},
{"label": "029-0006-0"},
{"label": "029-0022-0"},
{"label": "029-0097-0"},
{"label": "029-0286-0"},
{"label": "029-0339-0"},
{"label": "029-0352-0"},
{"label": "029-0362-0"},
{"label": "029-0407-0"},
{"label": "029-0461-0"},
{"label": "029-0463-0"},
{"label": "029-0681-0"},
{"label": "030-0035-0"},
{"label": "030-1045-2"},
{"label": "030-1046-0"},
{"label": "030-1107-3"},
{"label": "030-2002-0"},
{"label": "030-2227-2"},
{"label": "030-2248-0"},
{"label": "030-2295-0"},
{"label": "030-2367-0"},
{"label": "030-2705-0"},
{"label": "031-0105-0"},
{"label": "031-0106-0"},
{"label": "031-0112-2"},
];
</script>
<style>
li{
list-style-type: none;
color: #888;
}
li:hover{
color: #000;
font-weight: bold;
cursor: pointer;
text-decoration: underline;
}
.suggestions{
display: block;
position: relative;
width: 120px;
height: auto;
border: 1px solid #aaa;
background: #CCC;
left: 3px;
}
</style>
</head>
<body>
<input type="text" name="InputSpace" id="inputSpace">
<div class="suggestionsContainer">
<ul class="list">
<div class="suggestions">
</div>
</ul>
</div>
<script>
var typed;
var a = 1;
$("#inputSpace").keyup(function(){
typed = $("#inputSpace").val();
for(var x = 0; x < partsArray.length; x++){
if(partsArray[x].label.substring(0,a) == typed){
$(".suggestions").append("<li name='"+partsArray[x].label+"'>"+partsArray[x].label+"</li>");
}
else
{
continue;
}
}
a=a+1;
});
$("input").keypress(function(){
$(".suggestions").html("");
});
$(".list li").click(function(){
var number=$(this).attr("name");
$("input").val(number);
});
</script>
</body>
</html>