I have a field on which an autocomplete function is implemented. This is my code :
<%= f.text_field :auteur_1, size: 100, class:"champs_auteur", data: {autocomplete_source: auteurs_enum_path} %>
That creates the following html :
<input size="100" class="champs_auteur ui-autocomplete-input" data-autocomplete-source="/auteurs/enum" name="biblio[auteur_1]" id="biblio_auteur_1" autocomplete="off" type="text">
And this is in my javascript :
$(document).on('ready', function() {
return $('.champs_auteur').autocomplete({
source: $('.champs_auteur').data('autocomplete-source')
});
});
All that works fine. I am creating input fields using jquery after() and append(), like this :
$(document).on('ready', function () {
i = 2 ;
$(".extra_auteur").click(function(){
$('.premier_auteur').after('<div class="grid mbm"><div class="one-sixth"><label for="biblio_auteur">Auteur</label></div><input size="100" class="champs_auteur" data-autocomplete-source="/auteurs/enum" type="text" name="biblio[auteur_' + i++ + ']" id="biblio_auteur_1" /></div>');
});
});
This is minified code (mainly html) for :
<div class="grid mbm">
<div class="one-sixth">
<label for="biblio_auteur">Auteur</label>
</div>
<input size="100" class="champs_auteur" data-autocomplete-source="/auteurs/enum" type="text" name="biblio[auteur_' + i++ + ']" id="biblio_auteur_1" />
</div>
This does create the input field with an id="biblio_auteur_2" (and 3, 4 etc...).
The autocomplete doesn't work on the additional input fields. I don't see why not.
This is because you're using the element #ID instead of the class-name as in:
And #ID is always unique, and will therefore only work for 1 item. What yu need to do is have the jQuery select the CSS element class instead and not ID.
or whatever the class name is.
But you must be sure that the element which currently have the ID of aut_extra instead have a CSS class with the same name.