0

Autocomplete working perfect on first input element

<input type="text" id="inp01"></input>

but after making clone of this input autocomplete not working for new

<input type="text" id="inp02"></input>
autocm("inp02");

my autocomplete function is

function autocm(inputID){
  //tempdata have an array of strings
  $(document.body).find("#" + inputID).autocomplete({
        source: tempData
  });
};
Sumit Kumar
  • 394
  • 2
  • 20

2 Answers2

1

See the code snippet below, where I have used the jQuery UI along with jQuery UI autocomplete. The functionality works, Please ignore the missing style, you can easily fix it.

How it works:

At first, I cloned the input element and then rebind the autocomplete on it and then enable autocomplete on it. The snippet that you are interested is as follows:

    $(tag2).autocomplete({
        source: availableTags
    }).autocomplete('enable');

You can run and test the snippet. Please let us know whether it works for you or not.

Sample Run

  1. Run the Run code snippet
  2. Click on Try clone and bind. You will see input field with clone-* value by default. Here * will be incrementing numbers 2, 3, 4 etc.
  3. Click on clone-2 and try to change value to something starting with a, b. You will see autocomplete appearing on the fields.

Try to study the code and tweak it to your need. Let us know if you still feel confused on it.

$( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
  
    $("#tags").autocomplete({
      source: availableTags
    });
    
    var count   = 1;
    var cloneId = 'clone-';
    
    $('#tryClone').on('click', function() {
        var tag2 = $('#tags').clone();
        tag2.removeAttr("id");
        
        count += 1;
      
        var elementId = cloneId + count;
   
        $(tag2).attr("id", elementId).val(elementId);
        
        $(tag2).autocomplete({
            source: availableTags
        }).autocomplete('enable');
        

        $(tag2).appendTo('#container');
    });
    
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<link rel="stylesheet" href="//jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
  
  <div id="container">
  </div>
  
  <button id="tryClone">
      Try clone and bind
  </button>
</div>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Samundra
  • 1,869
  • 17
  • 28
  • Thanx @Samundra but this does't solve my problem. after some digging i got solution that first i need to remove autocomplete class from that element then again bind autocomplete. – Sumit Kumar Sep 21 '16 at 10:05
  • That's great. You found solution. By the way, the autocomplete plugin should have removed the `autocomplete` class by itself when its `close` event gets fired. – Samundra Sep 21 '16 at 10:18
  • No its not removed class. its just disable the autocomplete widget for that input element – Sumit Kumar Sep 21 '16 at 10:22
  • I agree. When I tested the autocomplete, input element had extra attribute `autocomplete=off` for cloned input. – Samundra Sep 21 '16 at 10:24
1

After digging i found this solution.

  1. Autocomplete add an class to a input element.
  2. When we cloned input element then that class also present already.
  3. so before initialise autocomplete to an cloned element we first need to remove that class "ui-autocomplete-input";
  4. then call autocomplete function on that input.
Sumit Kumar
  • 394
  • 2
  • 20