Clone auto complete not working as expected

Clone auto complete not working as expected

Hi all,

Currently working on jquery clone and auto complete hre is my detail code for jquery clone

  1. var num = $('.cloned-row1').length;
  2.    var newnum = num + 1;
  3.   $(document).on("click", ".edu_add_button", function() {
  4.     var $clone = $('.cloned-row1:eq(0)').clone(true, true).attr('id', "cloned-row" + newnum);
  5.     $clone.find('[id]').each(function() {
  6.       $(this).removeClass("errRed");   
  7.       if ($(this).hasClass("required_Field")) {
  8.         $(this).prevAll('label').find('span.required-star').removeClass('text-error-red');
  9.         $(this).addClass("cloned_field");
  10.         //$(this).addClass("errRed");
  11.       } else {
  12.         $(this).removeClass("errRed");
  13.         $(this).removeClass("text-error-red");
  14.       }
  15.     });
  16.     $clone.find('.btn_more').after("<input type='button' class='btn_less1 edu_btnle' id='buttonless'/>");
  17.     $clone.find(".school_Name").attr('disabled', true).val('');
  18. $clone.find(".txt_schName").val('').attr('id', 'txt_schName_' + newnum);
  19.     $clone.find(".degree_Description").attr('disabled', true).val('');
  20.     $clone.find(".ipt_Havg").val('');
  21.     $clone.find(".trans_date").val('');

  22.     var dateobj = new Date();
  23.     var datemonth;
  24.     if (dateobj.getMonth() + 1 < 10) datemonth = "0";
  25.     datemonth += dateobj.getMonth() + 1;
  26.     var fulldate = datemonth + "-" + dateobj.getDate() + "-" + dateobj.getFullYear();
  27.     $clone.find("input.deg_date")
  28.       .removeClass('hasDatepicker')
  29.       .removeData('datepicker')
  30.       .unbind()
  31.       .datepicker({
  32.         dateFormat: "mm/dd/yy",
  33.         changeMonth: true,
  34.         yearRange: "-100:+0",
  35.         changeYear: true,
  36.         maxDate: new Date(),
  37.         showButtonPanel: false,
  38.       });

  39.     $('.cloned_field').addClass("errRed");
  40.     var dateobj = new Date();
  41.     var datemonth;
  42.     if (dateobj.getMonth() + 1 < 10) datemonth = "0";
  43.     datemonth += dateobj.getMonth() + 1;
  44.     var fulldate = datemonth + "-" + dateobj.getDate() + "-" + dateobj.getFullYear();
  45.     $clone.find("input.trans_date")
  46.       .removeClass('hasDatepicker')
  47.       .removeData('datepicker')
  48.       .unbind()
  49.       .datepicker({
  50.         dateFormat: "mm/dd/yy",
  51.         changeMonth: true,
  52.         yearRange: "-100:+0",
  53.         changeYear: true,
  54.         maxDate: new Date(),
  55.         showButtonPanel: false,
  56.       });

  57.     $(this).parents('.educat_info').after($clone);
  58.  
  59. autoComplete($('#txt_schName_' + newnum));
  60.   });

Here is my auto complete code

  1. $.widget('custom.tableAutocomplete', $.ui.autocomplete, {
  2.     options: {
  3.         open: function (event, ui) {
  4.             // Hack to prevent a 'menufocus' error when doing sequential searches using only the keyboard
  5.             $('.ui-autocomplete .ui-menu-item:first').trigger('mouseover');
  6.         },
  7.         focus: function (event, ui) {
  8.             event.preventDefault();
  9.         },
  10. highlightClass: "ui-state-highlight"
  11.     },
  12.     _create: function () {
  13.         this._super();
  14.         // Using a table makes the autocomplete forget how to menu.
  15.         // With this we can skip the header row and navigate again via keyboard.
  16.         this.widget().menu("option", "items", ".ui-menu-item");
  17.     },
  18.     _renderMenu: function (ul, items) {
  19.         var self = this;
  20.         var $li = $("<li>");
  21. $table = $('<table class="table-autocomplete">'),
  22.             $thead = $('<thead>'),
  23.             $headerRow = $('<tr>'),
  24.             $tbody = $('<tbody>');

  25.         $.each(self.options.columns, function (index, columnMapping) {
  26.             $('<th>').text(columnMapping.title).appendTo($headerRow);
  27.         });

  28.         $thead.append($headerRow);
  29.         $table.append($thead);
  30.         $table.append($tbody);
  31. $li.append($table);
  32.         ul.html($li);

  33.         $.each(items, function (index, item) {
  34.             self._renderItemData(ul, ul.find("table tbody"), item);
  35.         });
  36.     },
  37.     _renderItemData: function (ul, table, item) {
  38.         return this._renderItem(table, item).data("ui-autocomplete-item", item);
  39.     },
  40.     _renderItem: function (table, item) {
  41.         var self = this;
  42.         var $tr = $('<tr class="ui-menu-item" role="presentation">');
  43.         $.each(self.options.columns, function (index, columnMapping) {
  44.             var cellContent = !item[columnMapping.field] ? '' : item[columnMapping.field];
  45.             $('<td>').text(cellContent).appendTo($tr);
  46.         });

  47.         return $tr.appendTo(table);
  48.     }
  49. });

  50.  $(document).ready(function() {
  51. autoComplete($('#txt_schName_1'));
  52. });

  53. function autoComplete(t) {
  54. t.tableAutocomplete({
  55. highlightClass: "bold",
  56. source: function(request, response) {
  57.  var regex = new RegExp(request.term, 'i');
  58.  //var filteredArray = filteredArray.slice(0,10);
  59.  $.ajax({
  60. url: "json/dummy.json",
  61. dataType: "json",
  62. data: {
  63.  term: request.term
  64. },
  65. success: function(data) {
  66.  response($.map(data, function(item) {
  67. // This code is only for testing. It should be done on the server!
  68. if (regex.test(item.id) || regex.test(item.label)) {
  69.  return item
  70. }
  71.  }));
  72. },

  73.  });
  74. },
  75. columns: [{
  76. field: 'id',
  77. title: 'Search School Name'
  78. }, {
  79. field: 'label',
  80. title: 'School Name'
  81. }],
  82. delay: 500,
  83. select: function (event, ui) {
  84. if (ui.item != undefined) {
  85. $(this).val(ui.item.value);
  86. $('#school_Name').val(ui.item.label);
  87. console.log(ui.item.label);
  88. if (ui.item.label === "Other"){
  89. var schoolObj = $(".school_Name");
  90.   schoolObj.prop('disabled', false);
  91.   schoolObj.val('');
  92. }
  93. }
  94. }
  95. });
  96. }
Here is my JSON value

  1. [{"id":"COL000001","label":"Emirates College of Technology- UAE","value":"COL000001"},
  2.  {"id":"COL000002","label":"Al Khawarizmi International College- UAE","value":"COL000002"},
  3.  {"id":"COL000003","label":"Syscoms College","value":"COL000003"},
  4.  {"id":"COL000004","label":"Abounajm Khanj Pre-Uni Center","value":"COL000004"},
  5.  {"id":"COL000005","label":"Advanced Placement","value":"COL000005"},
  6.  {"id":"COL000006","label":"Al Buraimi College (Uni Clge)","value":"COL000006"},
  7.  {"id":"COL000007","label":"Al-Ain Community College","value":"COL000007"},
  8.  {"id":"COL000008","label":"AMA Computer College","value":"COL000008"},
  9.  {"id":"COL000009","label":"Arab Academy for Bankg and Fin","value":"COL000009"},
  10.  {"id":"COL0000010","label":"ARABACDSCITECHMARTIMETRNS","value":"COL0000010"},
  11.  {"id":"COL0000011","label":"Arapahoe Community College","value":"COL0000011"},
  12.  {"id":"Other","label":"Other","value":"Other"}]

Html field

 <div class="container-fluid cloned-row1 cloned_div educat_info" id="cloned-row1" name="cloned-row1">
<input type="text" id="txt_schName_1" class="ipt_Field required_Field txt_schName "/>
<input type="button" class="btn_more edubtnmore btn_right edu_add_button" />
</div>


First time it was working fine but when i clik button in the second time it was not working