Hello,
I am trying to add a new field and when I do so, I want it to use an autocomplete function that I have created previously. At the moment, I am able to show a new field but when I start typing in the new field it is not performing the autocomplete function that I built.
Please provide me with any insight you might have.
Here is my code thus far. The php function for player names is at the top. Then there is the javascript autocomplete function. Then there is the javascript insert field script (this is the area where I am having trouble getting the autocomplete function to work), then there is the HTML:
I am trying to add a new field and when I do so, I want it to use an autocomplete function that I have created previously. At the moment, I am able to show a new field but when I start typing in the new field it is not performing the autocomplete function that I built.
Please provide me with any insight you might have.
Here is my code thus far. The php function for player names is at the top. Then there is the javascript autocomplete function. Then there is the javascript insert field script (this is the area where I am having trouble getting the autocomplete function to work), then there is the HTML:
Code:
<?
$limit = 2000;
$sql = sprintf( 'SELECT name, team, position, id FROM players LIMIT %d', $limit );
$result = mysql_query( $sql );
$players = array();
while( $row = mysql_fetch_object( $result ) ){
$players[] = array(
"id" => $row->id,
"name" => $row->name . ", " . $row->team . ", " . $row->position .""
);
}
?>
<body>
<script type="text/javascript">
var playersArray = <?= json_encode ( $players ); ?>;
var playersNames = [];
for(var i in playersArray){
playersNames.push(playersArray[i].name);
}
function getIdByName(_name){
for(var i in playersArray){
if(_name == playersArray[i].name){
return playersArray[i].id;
}
}
}
function validate(){
document.getElementById("tags").value = getIdByName(document.getElementById("tags").value);
return ($('#stat').val() == '');
}
$(document).ready(function(){
$("#tags").autocomplete({source: playersNames});
});
</script>
<script language="javascript">
fields = 0;
function addInput() {
if (fields != 10) {
document.getElementById('tags').innerHTML += "<input type='text' id='tags' name='p2' style='width:330px' value='<? $data['name']; ?>, <?= $data['team']; ?>, <?= $data['position']; ?>' /><br />";
fields += 1;
} else {
document.getElementById('tags').innerHTML += "<br />Only 10 players per comparison allowed.";
document.form.add.disabled=true;
}
}
</script>
<form name="form">
<input type="button" onclick="addInput()" name="tags" value="Add input field" />
</form>
<div id="tags">
</div>
</body>
Comment