Bisogna capire come funziona un programma. Prima di inserire l'elemento 1 questo non esiste visto che è creato dinamicamente. Quindi l'autocompletamento deve essere creato dopo della creazione dell'elemento.
Codice PHP:
<!DOCTYPE html><html>
<head>
<title>Untitled</title>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script>
var availableTags =[ "Andrea","Marco","Anna","Fernando" ];
function ac(anId) {
$(anId).autocomplete({ source: availableTags });
}
var indice =0;
function inserisci(){
indice++;
var html ="";
html ='<br/><div id="subDiv'+indice+'">Modulo:'+indice+'<br/>';
html = html +'Nome :<input type="text" name="nome'+indice+'" id="nome'+indice+'"/><br/>';
html = html +'Cognome :<input type="text" name="cognome'+indice+'"/><br/>';
html = html +'Indirizzo :<input type="text" name="indirizzo'+indice+'" /><br/>';
html = html +'Città :<input type="text" name="citta'+indice+'" /><br/>';
html = html +'CAP :<input type="text" name="cap'+indice+'" /><br/>';
html = html +'Provincia :<input type="text" name="provincia'+indice+'" /></div>';
document.getElementById('mydiv').innerHTML = document.getElementById('mydiv').innerHTML + html;
ac("#nome"+indice);
}
</script>
</head>
<body>
<input type="button" id="inserisci" value="inserisci" onclick="inserisci()">
<hr/>
<form name="modulo" action="link.php" method="post">
<div id="mydiv">
</div>
<input type="submit" name="invia" value="Invia">
</form>
<hr/>
</body>
</html>