Table insertRow() Méthode
Exemple
Insérez de nouvelles lignes à la première position d'un tableau (et insérez un élément <td> avec du contenu) :
// Find a <table> element with id="myTable":
var table = document.getElementById("myTable");
// Create an empty <tr> element and add it to the 1st position of the table:
var row = table.insertRow(0);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
// Add some text to the new cells:
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
Définition et utilisation
La méthode insertRow() crée un élément <tr> vide et l'ajoute à une table.
La méthode insertRow() insère la ou les nouvelles lignes à l'index spécifié dans la table.
Remarque : Un élément <tr> doit contenir un ou plusieurs éléments <th> ou <td>.
Astuce : Utilisez la méthode deleteRow() pour supprimer une ligne.
Prise en charge du navigateur
Method | |||||
---|---|---|---|---|---|
insertRow() | Yes | Yes | Yes | Yes | Yes |
Syntaxe
tableObject.insertRow(index)
Valeurs des paramètres
Value | Description |
---|---|
index | Required in Firefox and Opera, optional in IE, Chrome and Safari. A number that specifies the position of the row to insert (starts at 0). The value of 0 results in that the new row will be inserted at the first position. The value of -1 can also be used, this results in a new row being inserted at the last position. This parameter is required in Firefox and Opera, but optional in Internet Explorer, Chrome and Safari.If this parameter is omitted, insertRow() inserts a new row at the last position in Chrome, IE, Firefox and Opera, and at the first position in Safari. |
Détails techniques
Valeur de retour : | L'élément <tr> inséré |
---|
Plus d'exemples
Exemple
Créer et supprimer des lignes :
function myCreateFunction() {
var table = document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "NEW CELL1";
cell2.innerHTML = "NEW CELL2";
}
function myDeleteFunction() {
document.getElementById("myTable").deleteRow(0);
}
Pages connexes
Référence HTML : balise HTML <tr>
❮ Objet tableau