Soucis javascript => Incrémentation dans une fonction
nothinghere
Messages postés2Date d'inscriptionjeudi 22 avril 2004StatutMembreDernière intervention30 septembre 2005
-
30 sept. 2005 à 11:25
cs_bultez
Messages postés13615Date d'inscriptionjeudi 13 février 2003StatutMembreDernière intervention15 octobre 2013
-
30 sept. 2005 à 12:44
J'utilise le code suivant :
<script>
function ajoutLigne() {
// Nouveau TD (colonne de gauche)
var newData1 = window.parent.document.createElement('td');
newData1.innerHTML = '<textarea rows="3" name="titre" cols="20" class="textarea_pl"></textarea>';
newData1.align="center";
var newData2 = window.parent.document.createElement('td');
newData2.innerHTML = '<textarea rows="3" name="paragraphe" cols="20" class="textarea_pl"></textarea>';
newData2.align="center";
var newData3 = window.parent.document.createElement('td');
newData3.innerHTML = '<textarea rows="3" name="texte" cols="20" class="textarea_pl"></textarea>';
newData3.align="center";
var newData4 = window.parent.document.createElement('td');
newData4.innerHTML = '<textarea rows="3" name="histoire" cols="20" class="textarea_pl"></textarea>';
newData4.align="center";
var newData5 = window.parent.document.createElement('td');
newData5.innerHTML = '<textarea rows="3" name="date_debut" cols="20" class="textarea_pl"></textarea>';
newData5.align="center";
var newData6 = window.parent.document.createElement('td');
newData6.innerHTML = '<textarea rows="3" name="date_fin" cols="20" class="textarea_pl"></textarea>';
newData6.align="center";
var newData7 = window.parent.document.createElement('td');
newData7.innerHTML = '<textarea rows="3" name="lieu" cols="20" class="textarea_pl"></textarea>';
newData7.align="center";
var newData8 = window.parent.document.createElement('td');
newData8.innerHTML = '<textarea rows="3" name="nickname" cols="20" class="textarea_pl"></textarea>';
newData8.align="center";
// Nouveau TR
var newRow = window.parent.document.createElement('tr');
newRow.appendChild(newData1);
newRow.appendChild(newData2);
newRow.appendChild(newData3);
newRow.appendChild(newData4);
newRow.appendChild(newData5);
newRow.appendChild(newData6);
newRow.appendChild(newData7);
newRow.appendChild(newData8);
// Ajout du nouveau TR au tableau
document.getElementById("tableau").appendChild(newRow);
}
</script>
Petit soucis, je voudrais que le nom de mes textarea, une fois insérés grace à ce javascript, soient suivis d'un numéro (ainsi quand on insére le textarea "titre", on se retrouve, sur la page html, avec une ligne et un textarea qui aie le nom "titre1", et si on ajoute encore une autre ligne, on aura "titre2", etc ...)
Une idée ?
J'ai tenté :
<script>
var n=3;
function ajoutLigne() {
n++; // Nouveau TD (colonne de gauche)
var newData1 = window.parent.document.createElement('td');
newData1.innerHTML = '<textarea rows="3" name="POUET '+n+' " cols ="20" class= "textarea_pl"></textarea>';
newData1.align="center";
var newData2 = window.parent.document.createElement('td');
newData2.innerHTML = '<textarea rows="3" name="CHAMP '+n+' " cols ="20" class="textarea_pl"></textarea>';
newData2.align="center";
[...]
// Nouveau TR
var newRow = window.parent.document.createElement('tr');
newRow.appendChild(newData1);
newRow.appendChild(newData2);
newRow.appendChild(newData3);
newRow.appendChild(newData4);
newRow.appendChild(newData5);
newRow.appendChild(newData6);
newRow.appendChild(newData7);
newRow.appendChild(newData8);
// Ajout du nouveau TR au tableau
document.getElementById("tableau").appendChild(newRow);
}
</script>
Mais ça ne fonctionne pas !!
Aidez moi !
A voir également:
Soucis javascript => Incrémentation dans une fonction
cs_bultez
Messages postés13615Date d'inscriptionjeudi 13 février 2003StatutMembreDernière intervention15 octobre 201330 30 sept. 2005 à 12:44
Bonjour,
si on fait :
newData1.innerHTML = '<textarea rows="3" name="titre" cols="20" class="textarea_pl"></textarea>';
on aura donc plusieurs "titre" et on peut y accéder
avec titre[indice]
sinon faire :
varglobale++;
newData1.innerHTML = '<textarea rows="3" name="titre'+varglobale+'" cols="20" class="textarea_pl"></textarea>';
var varglobale=-1; étant définie hors d'une function et "avant".
Cordialement. Bul.