JQuery: how to clone autocomplete fields?

I am using the jQuerer Jรถrn Zaefferer jquery autocomplete plugin and I cannot figure out how to make it work when I clone the autocomplete field. It almost works, because the options are displayed in the cloned autocomplete field when I print the text, but I cannot select the elements. At first, I thought it was a browser compatibility issue, but it happens in both FF3 and Safari, so I assume I have lost the error.

Here is a working example of what I am doing:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Autocomplete Clone Demo</title>
<style>
body {
margin: 40px;
}
.hide_element {
display: none;
}
</style>
<link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"></script>
<script type="text/javascript">
    function setAutocomplete()
    {
        var users = [
          { name: "Fred", id: "1" },
          { name: "Barney", id: "2" },
          { name: "Wilma", id: "3" }
        ];

        $(".user_selector").autocomplete(users, 
            {
                mustMatch: true,
                matchContains: true,
                minChars: 2,
                formatResult: function(row) { return row.name; },
                formatItem: function(row, i, max) { return row.name; }
            }
        );
    }

    var current= 0;

    var addParticipantFields = function() 
    {
        current++;
        $newParticipant = $("#template").clone(true);
        $newParticipant.removeAttr("id");
        $newParticipant.removeClass("hide_element");
        $prefix = "extra" + current;
        $newParticipant.children("div").children(":input").each(function(i) {
            var $currentElem= $(this);
            $currentElem.attr("name",$prefix+$currentElem.attr("name"));
        });
        $newParticipant.appendTo("#participantsField");
        setAutocomplete();
    }

    $(document).ready(function() {
        setAutocomplete();
        $("#addParticipant").live("click", addParticipantFields);

    });
</script>

</head>
<body>
<h1>Test Autocomplete Cloning</h1>
<form id="demo" method="post" action="">
<fieldset id="participantsField">
<label>Participants</label>
<div class="participant">
    <input class="user_selector" name="user" size="30"/>
</div>
</fieldset>

<!-- This is the template for adding extra participants -->
<div class="participant hide_element" id="template">
    <input class="user_selector" name="_user" size="30"/>
</div>

<p><input type="button" id="addParticipant" value="Add Another Participant"></p>
<p><input class="button" type="submit" value="Submit"/></p>
</form>
</body>
</html>
+3
source share
2 answers

to do

$newParticipant = $("#template").clone(true);

So

$newParticipant = $("#template").clone();

Your example works for me in FF when you do not clone events on #template.

+3
source

Firstly:

$newParticipant.children("div").children(":input").length == 0

, .

$newParticipant.children()

. 1 chield. . .

+1

Source: https://habr.com/ru/post/1704537/


All Articles