So, I load the data from the database. Then when I click the data, I want the value of the textbox automatically changed into what I clicked. But it seems like it only applies to the first data,and when I click the 2nd or 3rd data, the textbox still show the first data
//the data that covered by <a> as clickable
<?php foreach ($getData as $key): ?>
<tr>
<td>
<a href="#!" onclick="myFunction()"><p id="getid"><?php echo $key->id; ?></p></a>
</td>
<td><?php echo $key->name; ?></td>
<td><?php echo $key->job; ?></td>
</tr>
<?php endforeach ?>
//the textbox
<input id="code" type="text" name="id" required>
<script type="text/javascript">
function myFunction() {
document.getElementById("code").value = document.getElementById("getid").innerHTML;
}
</script>