[JS] afficher/masquer : mon_tableau has no properties

afficher/masquer : mon_tableau has no properties [JS] - HTML/CSS - Programmation

Marsh Posté le 07-11-2005 à 14:32:24    

salut
 
novice en JS, je voudrais afficher/masquer un élément <span>, mais j'ai beau le tourner dans tous les sens, je n'y arrive pas.
je crée des formulaires dans une boucle PHP :

Code :
  1. echo '<input type="checkbox" id="cbonoff[' .$i. ']" onclick="javascript:visibleHidden(\'onoff\')">
  2. <label for="cbonoff[' .$i. ']"> Texte toujours affiché</label>
  3. <span id="offon['.$i.']">texte à afficher si checkbox ==FALSE</span>
  4. <span id="onoff['.$i.']">texte à afficher si checkbox ==TRUE</span>';

et ma fonction

Code :
  1. function visibleHidden(tab){
  2. for (i=0; i>document.getElementById(tab).length; i++){
  3.  if (document.getElementById('cbonoff['+i+']').checked == true){
  4.   document.getElementById('offon['+i+']').style.visibility = "visible";
  5.   document.getElementById('offon['+i+']').style.visibility = "hidden";
  6.  }else{
  7.   document.getElementById('onoff['+i+']').style.visibility = "hidden";
  8.   document.getElementById('offon['+i+']').style.visibility = "visible";
  9.  }
  10. }
  11. }

le rapport dans la console JS de firefox :
Erreur : document.getElementById(tab) has no properties
Fichier Source : http://127.0.0.1/test/js.js
Ligne : 2

et la ligne2 : for (i=0; i>document.getElementById(tab).length; i++){
 
merci pour votre aide

Reply

Marsh Posté le 07-11-2005 à 14:32:24   

Reply

Marsh Posté le 07-11-2005 à 14:44:07    

Je sais pas, ptet que getElementById ne renvoie rien, donc ta valeur est à "undefined" et undefined n'a pas de membres/propriétés [:dawa]
 
Enfin bon vu le code tu fais n'importe quoi, donc ça m'étonne pas tant que ça s'tu veux [:dawa]


Message édité par masklinn le 07-11-2005 à 14:45:47

---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 07-11-2005 à 16:18:11    

je sais pas, ptet que je suis ici pour avoir de l'aide afin de comprendre et ne pas refaire n'importe quoi... ;)  
enfin bon avec ton intervention je vais pas aller bien loin s'tu veux :pt1cable:

Reply

Marsh Posté le 07-11-2005 à 17:30:51    

  • Un ID doit être unique dans une page (getElementById ne renvoie donc qu'un seul et unique élément au mieux, l'intérêt de boucler dessus est donc faible)
  • Si tu n'as pas d'élément ayant l'id "onoff" (celui que tu fournis en argument à ta fonction) ça ne risque pas de fonctionner
  • Apprends à faire du javascript, à utiliser le DOM et à faire tes mises en place via window.onload, parce que là la majorité du code est à jeter [:dawa]


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed