1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table id="myTable">
<tr>
<td>Row1 cell1</td>
<td>Row1 cell2</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var sel_1g=document.createElement("select");
// on affiche en console l'element généré :
console.log(sel_1gt);
var table = document.getElementById("myTable");
var row = table.insertRow();
var cell1 = row.insertCell();
cell1.innerHTML = "Nouvelle cellule";
var sel_1gt=sel_1g.cloneNode(true);
// On affiche en console l'element généré :
console.log(sel_1gt);
document.body.appendChild(document.createElement("select"));
sel_1gt.setAttribute("id","idM_"+1);
// On ajout l'élément au DOM
cell1.appendChild(sel_1gt);
var id_1g=document.getElementById("idM_"+1);
// On affiche en console l'element récupéré:
console.log(id_1g);
}
</script>
</body>
</html> |
Partager