0

Sorry for asking something that has been already asked by people (for instance here :

jQuery autocomplete for dynamically created inputs)

but i cannot make work it despite the help i found on internet. So, i need to use Jquery autocomplete with dynamically created inputs. My code looks as follows:

$("#add_ligne2").live("click", function() { ...
         if (nb_ligne < 10) {
            var html = "";
            var next_ligne = last_ligne;
            html = '<tr rel="' + next_ligne + '">';
            html += '<td><input type="text" id="autoCompleteProjets' + next_ligne + '"/></td>';
            html += '</tr>';
            $("#content_tr").append(html);
            $('#autoCompleteProjets1', html).autocomplete(autocomp_opt);
         }
      }
      var autocomp_opt = {
         source: "/index/autocomplete",
         minLength: 2,
         select: function(event, ui) {
            /* console.log( ui.item ?
                 "Selected: " + ui.item.value + " aka " + ui.item.id :
                 "Nothing selected, input was " + this.value 
             );*/
            $('.hidden').val(ui.item.id);
         }
      }
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
BYU
  • 75
  • 1
  • 13

1 Answers1

0

You use fixed id "autoCompleteProjets1", I believe you want "autoCompleteProjets" + next_ligne

$('#autoCompleteProjets' + next_ligne, html).autocomplete(autocomp_opt);

You might already have it, but I'll mention it anyway - make sure you initialize last_ligne outside of the anonymous function, else you create local variable, which is unset once out of scope:

var last_ligne = 0;

Then, you don't increment the last_ligne variable, use this:

var next_ligne = ++last_ligne;

And most important: use firebug in firefox, or equivalent for other browsers, to find out what you really generate.

Marek
  • 7,337
  • 1
  • 22
  • 33
  • I used this right after "$("#content_tr").append(html);" but it's not working unfortunately – BYU Jun 10 '13 at 10:11
  • Now I see you don't increment next_ligne, nor assign it to last_ligne, I'll update my answer. – Marek Jun 10 '13 at 11:47
  • The incrementation was already implemented but not visible in the block of code i posted. it's still not working unfortunately .... – BYU Jun 10 '13 at 12:46
  • then you have to post the whole code. Do you use some debugging techniques? – Marek Jun 10 '13 at 13:14