I have a working code where I can dynamically add input fields which can be used for auto-completion using AJAX. Though working, there are limitations. After adding more fields, placement of the autofill is incorrect, as demonstrated in this image:

The results are not showing under the current input field but rather under the last one. Lastly, once the user adds too many input fields and starts removing them, the autocomplete feature stops working altogether.
HTML Code:
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Case Category <button style="margin-top: 5px;" id = "add_field" class="add_field btn btn-primary btn-xs">+</button></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input id="search_keyword_idd" class="search_keywordd form-control col-md-5 col-xs-12" name="category[]" required="required" type="text">
<input type="hidden" name="catID[]" id="catID"/>
<div id="resultd"></div>
</div>
</div>
<div class = "t"></div>
Javascript/jQuery Pt. 1: (on the first input field)
<script type="text/javascript">
$(function(){
$(".search_keywordd").keyup(function()
{
var search_keyword_value = $(this).val();
var dataString = 'search_keyword='+ search_keyword_value;
if(search_keyword_value!='')
{
$.ajax({
type: "POST",
url: "../resources/ajax-search/case_category.php",
data: dataString,
cache: false,
success: function(html)
{
$("#resultd").html(html).show();
}
});
}
return false;
});
jQuery("#resultd").on("click", ".show", function(e){
var showName = $('.returnName',this).text();
var showId = $('.returnID',this).text();
$('#search_keyword_idd').val(showName);
$('#catID').val(showId);
});
jQuery(document).on("click", function(e) {
var $clicked = $(e.target);
if (! $clicked.hasClass("search_keywordd")){
jQuery("#resultd").hide();
}
});
$('#search_keyword_idd').click(function(){
jQuery("#resultd").show();
});
});
</script>
Javascript/jQuery Pt. 2: (on the input fields that the user want to add)
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper3 = $(".t"); //Fields wrapper
var add_button3 = $("#add_field"); //Add button ID
var z = 1; //initlal text box count
$(add_button3).click(function(e){ //on add input button click
e.preventDefault();
if(z < max_fields){ //max input box allowed
z++; //text box increment
$(wrapper3).append('<div class="item form-group"><label class="control-label col-md-3 col-sm-3 col-xs-12"></label><div class="col-md-6 col-sm-6 col-xs-12"><input id="search_keyword_idd'+z+'" class="search_keywordd'+z+' form-control col-md-5 col-xs-12" name="category['+z+']" required="required" type="text"><input type="hidden" name="catID['+z+']" id="catID'+z+'"/><div id="resultd'+z+'"></div><button class="remove btn btn-dark">Remove</button></div></div>'); //add input box
$("#resultd"+z+"").css({"margin-top": "40px", "position": "absolute", "display": "none", "border-top": "0px", "overflow": "visible", "border": "1px #F0F0F0 solid", "float": "left", "padding": "0"});
//$(".show"+z+"").css("cursor:", "default", "margin:", "0", "display:", "none", "background:", "#F7F7F7", "width:", "548px", "border-bottom:", "#F0F0F0 1px solid", "position:", "relative", "z-index:", "10");
}
$(".search_keywordd"+z+"").keyup(function() {
var search_keyword_value = $(this).val();
var dataString = 'search_keyword='+ search_keyword_value;
if(search_keyword_value!='') {
$.ajax({
type: "POST",
url: "../resources/ajax-search/case_category.php",
data: dataString,
cache: false,
success: function(html)
{
$("#resultd"+z+"").html(html).show();
}
});
}
return false;
});
jQuery("#resultd"+z+"").on("click", ".show", function(e){
var showName = $('.returnName',this).text();
var showId = $('.returnID',this).text();
$('#search_keyword_idd'+z+'').val(showName);
$('#catID'+z+'').val(showId);
});
jQuery(document).on("click", function(e) {
var $clicked = $(e.target);
if (! $clicked.hasClass("search_keyword"+z+"")){
jQuery("#resultd"+z+"").hide();
}
});
$('#search_keyword_idd'+z+'').click(function(){
jQuery("#resultd"+z+"").show();
});
$(wrapper3).on("click",".remove", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').parent('div').remove(); y--;
});
});
});
PHP:
<?php
include('config.php'); //working just fine
if($_POST)
{
if($_POST['search_keyword']) // returns an error from answer 1
{
$similar = mysql_real_escape_string($_POST['search_keyword']);
$result=mysqli_query($conn, "SELECT * FROM casecategory WHERE (caseCategoryName like '" . $_POST["search_keyword"] . "%')");
if (mysqli_num_rows($result) > 0) {
while($row=mysqli_fetch_array($result))
{
?>
<div class="show" align="left">
<span class="returnName"><?php echo $row["caseCategoryName"] ?></span>
<span class="returnID" style="display:none"><?php echo $row['idCaseCategory'];?></span>
</div>
<?php
}
}
else {
?>
<div class="show" align="left">
<span class="returnMessage">No matching records found.</span>
</div>
<?php
}
}
mysqli_close($conn);
}
?>
I am at a loss as to which part(s) are not working and how to fix it so that:
- The auto-complete box displays under the current
onfocusinput - When max-amount of inputs are added and then removed, that the auto-complete feature still works
$("#resultd").html(html).show();? If so that is why it stays where it does. I populates the same container.