K
K
Konstantin Kandaurov2014-10-13 19:32:05
JavaScript
Konstantin Kandaurov, 2014-10-13 19:32:05

How to "friend" jquery ui autocomplete with dynamic form?

I'm making a dynamic form with autocomplete values ​​from the database.

<FORM class = "filmfields" name = "edit" method="post" action = "<?=$_SERVER['REQUEST_URI']?>" enctype="multipart/form-data">
<INPUT type = "text" name = "name[]" id= "name" value = "" size = "50"><BR>
<SCRIPT>_tmpl_name='<INPUT type = "text" name = "name[]" id= "name" value = "" size = "50"><BR>';</SCRIPT>
<INPUT type = "button" value = "+" onclick = "$(this).before(_tmpl_name);">
</FORM>

Autocomplete only works on a field that loads immediately.
How to "fasten" autocomplete to those form elements that appear after clicking on the "+" button?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kandaurov, 2014-10-14
@kandkonst

And now attention! Correct answer:

$(document).on("focus",[selector],function(e) {
    if ( !$(this).data("autocomplete") ) { // If the autocomplete wasn't called yet:
        $(this).autocomplete({             //   call it
            source:['abc','ade','afg']     //     passing some parameters
        });
    }
});

var count = 0;
$(cloneButton).click(function() {
    var newid = "cloned_" + (count++); // Generates a unique id
    var clone = $(source) // the input with autocomplete your want to clone
        .clone()
        .attr("id",newid) // Must give a new id with clone
        .val("") // Clear the value (optional)
        .appendTo(target);
});

Source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question